What are the two basic output methods in php

青灯夜游
Release: 2023-03-13 19:46:01
Original
3599 people have browsed it

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.";
?>
Copy after login

Effect:

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

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]}";
?>
Copy after login

Effect:

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

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!";
?>
Copy after login

Effect:

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

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]}";
?>
Copy after login

Effect:

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

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!

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