954,585 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Echo a Variable

I've been echoing variables by simply typing "echo" then the variable. For instance:

<?php  
$name =  "Me";
 echo $name;
?>

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
$username = $_POST['username'];
$password = $_POST['password'];

echo "{username} : {$password}";
?>


Did he do it like that because of the POSTS command? Was this necessary? Help me understand, please? Thanks

Reliable
Junior Poster
152 posts since Jul 2007
Reputation Points: 11
Solved Threads: 3
 

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:

echo "My name is {$_GET['name']}";

instead of this:

echo "My name is " . $_GET['name'] . "";
Will Gresham
Master Poster
755 posts since May 2008
Reputation Points: 96
Solved Threads: 125
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You