Example tutorial of echo and print statements in PHP

王林
Release: 2023-04-07 19:52:01
Original
2558 people have browsed it

Example tutorial of echo and print statements in PHP

In PHP, there are two basic output methods: echo and print.

The difference between echo and print

echo can output more than one string.

print can only output a string and always returns 1.

Tip: echo is slightly faster than print because it does not return any value.

PHP echo statement

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

Display strings

The following example shows how to use the echo command to display different strings (also please note that the string can contain HTML tag):

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

Display variables

The following example shows how to use the echo command to display strings and variables:

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

PHP print statement

#print is also a language structure and can be used with or without parentheses: print or print().

Display string

The following example shows how to use the print command to display different strings (please also note that the string can contain HTML tag):

<?php
print "<h2>PHP is fun!</h2>";
print "Hello world!<br>";
print "I&#39;m about to learn PHP!";
?>
Copy after login

Display variables

The following example shows how to use the print command to display strings and variables:

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

Recommended tutorial: PHP tutorial

The above is the detailed content of Example tutorial of echo and print statements in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!