PHP Variables Echo
PHP Echo() Function is used to display the output of your program. It is not a function so, it is not necessary to use parenthesis after echo. If you want to passed more than one variable to display then it shouldn't be enclosed within parenthesis. Echo() doesn't react like a function, it is moreover used as a language constructor. It can output all types of data and multiple outputs can be through only one echo() command. For example :
$hello = "Hello People!"; echo $hello ; echo " to the world of web development.";
The output of the above example is : Hello People! to the world of web development.
The above example is so simple, we store the a string value in $hello variable and in the next line, I used echo() command to display the output. The other line also display the string value without using variable. It means you can display multiple number of output with the help of echo command. Suppose, you have multiple variable and string, integers, array or class and want to display without using echo command again and again. Let see the example :
$hello = "Hello People"; $name = "Suraj Mittal"; $age = "22"; echo $hello." My name is ".$name.". I am ".$age." years old, working as a Web Developer.";
The output of the above example is : Hello People My name is Suraj Mittal. I am 22 years old, working as a Web Developer.
In the above example we used multiple numbers of variables to display using only one echo command and concatenate to all of them with the help of dot operator. Lets move towards little bit complex programming. For example :
$today = date("d-m-y"); $time = date("H:i:s"); $browser = $HTTP_USER_AGENT; $name = "Suraj Mittal"; echo "Hello ".$name; echo " It is ".$time." and the Date is ".$today. ". Your Browser is ".$browser;
The output of the above example is : Hello Suraj Mittal! It is 06:25:28 and the Date is 12-01-10. Your Browser is Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:22.214.171.124) Gecko/20091221 Firefox/3.5.7
If you notice in the above we used date function to display the today date and time and $browser variable to display the version of your browser and rest of the items are same.
Posted on: January 12, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles