Home > Backend Development > PHP Tutorial > How to achieve output in PHP? What are the output methods of PHP?

How to achieve output in PHP? What are the output methods of PHP?

青灯夜游
Release: 2023-04-04 15:34:02
Original
19652 people have browsed it

In PHP, you can use echo statement, print statement, printf(), print_r(), var_dump() to achieve output. Let's take a look at these output methods. I hope it will be helpful to everyone.

How to achieve output in PHP? What are the output methods of PHP?

echo statement

echo is a language construct that can be used with or without parentheses: Echo or echo() can be used for output, and it is the most commonly used output statement. Example:

<?php
echo "PHP中文网";
echo ("m.sbmmt.com");
?>
Copy after login

Output:

How to achieve output in PHP? What are the output methods of PHP?

Description: In normal times, the output of echo or echo() is the same, only when multiple parameters are passed to Echo statement, when outputting, using parentheses will generate a parsing error.

print statement

The print statement can also achieve output and has a return value. It returns 1 on success and 0 on failure; therefore it can be used in expressions.

<?php
print "hello ";
print ("world!");
?>
Copy after login

Note: The print statement can also be used with or without parentheses, there is not much difference.

Output:

How to achieve output in PHP? What are the output methods of PHP?

printf() function

printf() function also Output can be achieved, ordinary strings can be output, and output can also be formatted. Example:

<?php
$a=20;
printf("输出:");
printf("a=%f",$a);
?>
Copy after login

Output:

How to achieve output in PHP? What are the output methods of PHP?

print_r()

print_r() Functions are used to output variables and can display information about this variable in a format that is easy for humans to read.

<?php
$a = array (&#39;a&#39; => &#39;apple&#39;, &#39;b&#39; => &#39;banana&#39;, &#39;c&#39; => array (&#39;x&#39;,&#39;y&#39;,&#39;z&#39;));
echo "<pre class="brush:php;toolbar:false">";
print_r ($a);
echo "
Copy after login
"; ?>

Output:

How to achieve output in PHP? What are the output methods of PHP?

var_dump()

var_dump() function For output variables, you can display the structured information of the variable, such as type and value.

<?php
$var_name=array(99,&#39;w3resource&#39;,67899.99, array(&#39;X&#39;,&#39;Y&#39;,1));
var_dump($var_name);
?>
Copy after login

Output:

How to achieve output in PHP? What are the output methods of PHP?

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's learning.

The above is the detailed content of How to achieve output in PHP? What are the output methods of 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