Web Info and IT Lessons Blog...

Sunday 26 October 2014

Echo and Print statements in PHP

This is the third lesson in PHP Lessons series. The previous lesson of PHP Lessons series was about creating and executing a PHP file and in this lesson we will learn about echo and print statements in PHP.

Echo and Print

Echo and print statements in PHP are used to show output on the screen like a string or an integer value. Let us say we have a string "Quick brown fox jumps over the lazy dog" which we want to output in our web browser then this is how we can do it:


echo "Quick brown fox jumps over the lazy dog";

The above code shows the string output using echo method. Now let us do it using print method:


print "Quick brown fox jumps over the lazy dog";

Note: PHP code starts with <?php and ends with ?>

Anything written between the opening and closing php tags is treated as php code.

Difference between echo and print statements:


The main difference between the echo and print statements is that echo statement can output more than one strings while print statement can output only one string. We can output more than one string in PHP using concatenation.

Concatenation in PHP


Let us say we have two strings variables str1 and str2 that we want to output using concatenation.


$str1 = 'This is first string';
$str2 = 'This is second string';

echo $str1.' and '.$str2;

The above code shows concatenation of $str1 with the string ' and ' which in turn is concatenated with $str2.



No comments:

Post a Comment