Home >Backend Development >PHP Problem >What are the php output methods?
php output method: 1. Use the echo statement to output, for example "echo 'hi!'"; 2. Use the print statement to output; 3. Use the printf() function to output, for example "printf("hi") "; 4. Use print_r() to output; 5. Use var_dump() to output.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
PHP output method
echo
echo is a language structure that can be used without or with parentheses. : echo or echo(). (Strings can contain HTML tags)
echo 'helloworld!';
print statement
print is also a language structure, brackets may or may not be used Brackets: print or print().
print('helloworld!'); //输出成功返回1,失败返回0。
printf() function
The printf() function can also realize output, and can output ordinary strings and formatted output. Example:
<?php $a=20; printf("输出:"); printf("a=%f",$a); ?>
Output:
输出:a=20.000000
print_r()
print_r Displays easy-to-understand information about a variable Information, if string, integer or float is given, the variable value itself will be printed.
If array is given, the keys and elements will be displayed in a certain format. object is similar to an array.
1. You must add brackets when using: print_r().
2. print_r() will move the pointer of the array to the end. Use reset() to return the pointer to the beginning.
<?php $a = array ('a' => 'apple', 'b' => 'banana', 'c' => array ('x','y','z')); echo "<pre class="brush:php;toolbar:false">"; print_r ($a); echo ""; ?>
Output:
Array ( [a] => apple [b] => banana [c] => Array ( [0] => x [1] => y [2] => z ) )
var_dump()
Print the relevant information of the variable, this Functions display structural information about one or more expressions, including the expression's type and value. Arrays will expand values recursively, showing their structure through indentation.
<?php $var_name=array(99,'w3resource',67899.99, array('X','Y',1)); var_dump($var_name); ?>
Output:
array (size=4) 0 => int 99 1 => string 'w3resource' (length=10) 2 => float 67899.99 3 => array (size=3) 0 => string 'X' (length=1) 1 => string 'Y' (length=1) 2 => int 1
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of What are the php output methods?. For more information, please follow other related articles on the PHP Chinese website!