PHP 5 echo and print Statements
There are two basic ways to output data in PHP: echo and print.
In this chapter we will discuss the usage of both statements in detail and demonstrate how to use echo and print in examples.
PHP echo and print Statements
Differences between echo and print:
- echo - can output one or more strings
- print - allows only one string to be output, and always returns 1
**Tip:** echo is faster than print; echo has no return value, while print has a return value of 1.
PHP echo Statement
echo is a language construct; it can be used with or without parentheses: echo or echo().
Displaying Strings
The example below demonstrates how to use the echo command to output a string (the string can contain HTML tags):
Example
PHP is fun! " ; echo " Hello world!" ; echo " I want to learn PHP!
" ; echo " This is a " , " string, " , " uses " , " multiple " , " parameters. " ; ?>
Try it »
Displaying Variables
The example below demonstrates how to use the echo command to output variables and strings:
Example
" ; echo " learning PHP at $txt2 " ; echo "" ; echo " My car brand is {$cars[0]} " ; ?>
Try it »
PHP print Statement
print is also a language construct; it can be used with or without parentheses: print or print().
Displaying Strings
The example below demonstrates how to use the print command to output a string (the string can contain HTML tags):
Example
PHP is fun! " ; print " Hello world!" ; print " I want to learn PHP! " ; ?>
Try it »
Displaying Variables
The example below demonstrates how to use the print command to output variables and strings:
Example
" ; print " learning PHP at $txt2 " ; print "" ; print " My car brand is {$cars[0]} " ; ?>
Try it »