Hello Coders,
Today we explore most usable statement in PHP called echo and print.
Before we start I want to make sure that you install PHP in your local system and also go through this basic articles.
For that this articles may help you:
What is PHP? How to install it on windows? Full Guide
Unlike other programming language PHP don’t have any pre-declaration for output.
To give some output of the program we use echo or print statement in PHP.
The PHP echo Statement
In PHP echo statement used with or without parentheses: echo or echo().
Let’s understand echo statement with example:
<?php
$txt= "askdiary.com is learning blog.";
$x = 3;
$y = 2;
echo "Hello world!<br>";
echo "<h2>Let's Learn PHP.</h2>";
echo "multiple parameters","example","in echo php.";
echo "<h2>" . $txt. "</h2>";
echo "sum of x and y: ".$x + $y;
?>
Output of above example:
Hello world!Let’s Learn PHP.
multiple parameters example in echo php.askdiary.com is learning blog.
sum of x and y: 5The PHP print Statement
In PHP echo statement used with or without parentheses: print or print().
Let’s understand print statement with example:
<?php
$txt= "askdiary.com is learning blog.";
$x = 3;
$y = 2;
print "Hello world!<br>";
print "<h2>Let's Learn PHP.</h2>";
print "multiple parameters example in echo php.";
print "<h2>" . $txt. "</h2>";
print "sum of x and y: ".$x + $y;
?>
Output of above example:
Hello world!Let’s Learn PHP.
multiple parameters example in echo php.askdiary.com is learning blog.
sum of x and y: 5As you see from the above example echo and print statement work same for output right!!
So what is the difference between echo and print is the question I know you have in your mind.
Let’s see that..
Difference between echo and print
The main difference between echo and print is that execution of echo is more faster then print statement.
And other difference is that echo does not return any value but print always returns an integer value, which is 1.
And the last difference is that We can pass multiple strings separated by comma (,) in echo. But cannot pass multiple arguments in print statement.
And that’s it..
I hope this article is clear all concept of echo and print statements.
If you have any query on this then let me know by commenting below.
Happy to help you.
Thank You.
Leave a Reply