Home » Web development » PHP » PHP echo construct

PHP echo construct

Today I had learned about echo in PHP , it is not a function , it is a language construct. By language construct I mean that meaning that it is an integral part of PHP itself. It allows us to output data on the users web browser. You can output values stored in a variable using echo or you can also output string data using echo. We can use both single quotation mark ‘ ‘ or double quotation mark ” “ while using echo. Double quotation mark are used when you want to output value of some variable to the browser in a program and your line must terminate with the semi colon ;

<?php
echo "!I am echo construct!";
?>

Now this would output !I am echo construct! to the web browser. Now the semi-colan (;) at the end of the echo command is used to mention the end of the echo construct. Without this semi-colon PHP would report an error.
So previous example shows the use of echo with double quotation marks. As stated earlier we can also use it with single quotation marks as shown below

<?php
echo '!I am echo construct!';
?>

Now this would also output !I am echo construct! to the web browser.

In PHP we can also use echo to do arithmetic as can be seen in following example

Here line 2 is

<?php
echo 20+20;
echo '20+20';
?>

echo 20+20;

This would output 40. This means echo command can be used for doing arithmetic and here note that quotation marks have not been used. You can also subtract , do division and multiplication using this echo construct.

See also  include or require function in PHP with practical examples

Here line 3 is

echo ’20+20′;

This would literally output 20+20. This happens because quotation marks were used, this was processed as a string instead of a mathematical operation. And same thing would happen if we use double quotation marks instead of single quotation marks.

Hope you like post on echo in PHP

Also Reads
How to run php file in windows : This post talks about how we can php file on the the window and do the testing before moving to the web server
uses of PHP : This has detailed description why PHP is so much used through out the world.
first php program : This is good starting point for anybody starting with PHP and talk in good detail about how to write your PHP program and execute it
Define constant in PHP
PHP String Functions
PHP Array functions
https://en.wikipedia.org/wiki/PHP

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top