Home  >  Article  >  Backend Development  >  What are the two basic output methods in php

What are the two basic output methods in php

青灯夜游
青灯夜游Original
2021-11-11 19:05:153558browse

The two basic output methods of php are "echo" and "print". Echo is used to output one or more strings and can accept multiple parameters and has no return value. The syntax is "echo($str)"; while print is used to output a string and can only accept one parameter and has a return value. The syntax is "echo($str)". "print($str)".

What are the two basic output methods in php

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

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

Difference between echo and print

  • echo - Outputs one or more strings, can accept multiple arguments and No return value
  • print - can only output a string, can only accept one parameter and have a return value, and always returns 1

Tip: echo is slightly faster than print because It returns no value.

PHP echo statement

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

2. Display string , the following example shows how to use the echo command to display different strings (please also note that the string can contain HTML tags)

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

Effect:

PHP is fun!
123
我爱php
Hello world!
I&#39;m about to learn PHP!
This string was made with multiple parameters.

3.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 <br />";
echo "My car is a {$cars[0]}";
?>

Effect:

Learn PHP
Study PHP at W3School.com.cn 
My car is a Volvo

PHP print statement

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

2. Display strings. The following example shows how to use the print command to display different strings (please note that the string can contain HTML tags):

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

Effect:

PHP is fun!
Hello world!
I&#39;m about to learn PHP!

3. 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 <br>";
print "My car is a {$cars[0]}";
?>

Effect:

Learn PHP
Study PHP at W3School.com.cn 
My car is a Volvo

Recommended learning: "PHP Video Tutorial

The above is the detailed content of What are the two basic output methods in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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