Monday 5 August 2019

PHP echo construct

Today I had learned about PHP echo construct 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!";
?>
view rawecho-in-php1 hosted with ❤ by GitHub
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!';
?>
view rawecho-in-php2 hosted with ❤ by GitHub

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
<?php
echo 20+20;
echo '20+20';
?>
view rawecho-in-php3 hosted with ❤ by GitHub

Here line 2 is
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.
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.

0 comments:

Post a Comment