PHP echo and print statements

There are two basic output methods in PHP: echo and print.

In this chapter we will discuss the usage of the two statements in detail and demonstrate how to use echo and print in examples.

PHP echo and print statements

The difference between echo and print:

echo - can output one or more strings

print - Only one string is allowed to be output, and the return value is always 1

Tips: echo outputs faster than print, echo has no return value, and print has a return value of 1.

PHP echo statement

echo is a language structure, which can be used without parentheses or with parentheses: echo or echo().

Display string

The following example demonstrates how to use the echo command to output a string (the string can contain HTML tags):

Example

<?php
echo "<h2>PHP is fun!</h2>";
echo "Hello world!<br>";
echo "I'm about to learn PHP!<br>";
echo "This", " string", " was", " made", " with multiple parameters.";
?>

Try it»

Display variables

The following example demonstrates how to use the echo command to output variables and strings:

Example

<?php
$txt1="Learn PHP";
$txt2="php.cn";
$cars=array("Volvo","BMW","Toyota");
echo $txt1;
echo "<br>";
echo "Study PHP at $txt2";
echo "My car is a {$cars[0]}";
?>

Try it»

echo "<br>"; means outputting a newline

PHP print statement

print is also a language structure and can be used Parentheses, or without them: print or print().

Display string

The following example demonstrates how to use the print command to output a string (the string can contain HTML tags):

Example

<?php
print "<h2>PHP is fun!</h2>";
print "Hello world!<br>";
print "I'm about to learn PHP!";
?>

Try it»

Display variables

The following example demonstrates how to use the print command to output variables and strings:

Example

<?php
$txt1="Learn PHP";
$txt2="php.cn";
$cars=array("Volvo","BMW","Toyota");
print $txt1;
print "<br>";
print "Study PHP at $txt2";
print "My car is a {$cars[0]}";
?>

Try it»


Continuing Learning
||
<?php echo "<h2>PHP is fun!</h2>"; echo "Hello world!<br>"; echo "I'm about to learn PHP!<br>"; print "<h2>PHP is fun!</h2>"; print "Hello world!<br>"; print "I'm about to learn PHP!"; ?>
submitReset Code
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!