944,144 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 407
  • PHP RSS
Oct 13th, 2009
0

Echo a Variable

Expand Post »
I've been echoing variables by simply typing "echo" then the variable. For instance:
PHP Syntax (Toggle Plain Text)
  1. <?php
  2. $name = "Me";
  3. echo $name;
  4. ?>
This has been working just fine. I was watchign a tutorial and when he echoed his variable he put his in curly brackets. This is the example:

PHP Syntax (Toggle Plain Text)
  1. <?php
  2. $username = $_POST['username'];
  3. $password = $_POST['password'];
  4.  
  5. echo "{username} : {$password}";
  6. ?>

Did he do it like that because of the POSTS command? Was this necessary? Help me understand, please? Thanks
Similar Threads
Reputation Points: 11
Solved Threads: 3
Junior Poster
Reliable is offline Offline
152 posts
since Jul 2007
Oct 13th, 2009
0
Re: Echo a Variable
Its not needed there, there are a couple of uses for the braces, such as variable variables, but I believe the most common use is to include array vars in a string.

For example:
PHP Syntax (Toggle Plain Text)
  1. echo "My name is {$_GET['name']}<br />";
instead of this:
PHP Syntax (Toggle Plain Text)
  1. echo "My name is " . $_GET['name'] . "<br />";
Reputation Points: 96
Solved Threads: 124
Master Poster
Will Gresham is offline Offline
728 posts
since May 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Reliable / Suitable refresh, redirect ?
Next Thread in PHP Forum Timeline: Profile user page





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC