Objective
√ What is PHP ECHO and PRINT
√ How to use Echo and Print in PHP script
PHP Echo and Print, both are used for displaying text, string and variables value to browser. It is very easy to use it in PHP programming. You can use PHP Echo and Print like this.
<?php echo "Hello Move2Code.com"; print "Hello Move2Code.com"; ?>
Complete Programming Example
<!DOCTYPE html> <html> <head> <title>PHP Echo and Print</title> </head> <body> <h1>PHP Echo and Print</h1> <?php $str="Hello world"; $num=239490; $Days=Array("sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"); //Echo echo '<h3 style="color:green">Echo Example</h3>'; echo '<h3>'.$str.'</h3>'; echo $num; echo "<br>"; echo $Days[3]; echo "<br>----------------------------------"; //Print print '<h3 style="color:green">Print Example</h3>'; print '<h4>'.$str.'</h4>'; print $num; print "<br>"; print $Days[3]; print "<br>-----------------------------------"; ?> </body> </html>Output
Echo Example
Hello world239490
Wednesday
———————————-
Print Example
Hello world239490
Wednesday
———————————–
More fact about Echo and Print
ECHO STATEMENT
- Echo and Print, both are PHP Statement.
- Both are used for displaying text, string or variables values on the browser.
- Echo statement can be used with or without parenthesis(). For Example,
echo "Hello World"
andecho ("Hello World")
are same. - Echo can pass multiple strings with comma separator. For Example
echo ("This is ",$str," How are you")
. It will print This is Hello World. How are you. - Echo is faster than Print Statement.
- You can use dot
(.)
or comma(,)
as a joiner to join string.
Echo Example
<!DOCTYPE html> <html> <head> <title>PHP Echo and Print</title> </head> <body> <h1>PHP Echo and Print</h1> <?php $str="Hello world"; $num=239490; $Days=Array("sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"); //Echo echo ('<h3 style="color:green">Echo Example</h3>'); echo $str," ",$num," ",$Days[3]; ?> </body> </html>Output
Echo Example
Hello world239490
Wednesday
PRINT STATEMENT
- Print also can be used with or without parenthesis. For example,
print "Hello World"
andprint("Hello world")
are same. - Print cannot pass multiple arguments.
- Print statement behaves like a function and always return 1. So, it is quite slower than Echo.
- Print statement can be used in more complex programming structure.
- You can use dot
(.)
or comma(,)
as a joiner for join string.
<!DOCTYPE html> <html> <head> <title>PHP Echo and Print</title> </head> <body> <h1>PHP Echo and Print</h1> <?php $str="Hello world"; $num=239490; $Days=Array("sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"); //Echo print ('<h3 style="color:green">Print Example</h3>'); $result = print $str." ".$num." ".$Days[3]; print "<br>Result = ".$result; ?> </body> </html>
Print Example
Hello world239490
Wednesday
Result = 1
SUMMARY
In this tutorial you learn how to use PHP Echo and Print. You also learned the basic difference between Echo and Print in PHP. In the next chapter you will learn PHP Variables and Datatypes.