Home >Backend Development >PHP Problem >What are the commonly used methods for printing variables in PHP?
Commonly used methods are: 1. Use echo(), the syntax "echo($var)"; 2. Use var_dump(), the syntax "var_dump($var)"; 3. Use print(); 4 , use print_r(); 5. use printf(); 6. use sprintf().
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
A common way to print variables:
1. echo()
echo outputs one or more strings, which is a PHP statement. It is not a function, so it has no return value
<?php $name = '张三'; echo($name); ?>
2、var_dump()
var_dump() Used to display structural information about one or more expressions, print the type, value, and length of the variable
<?php $name = '张三'; var_dump($name); ?>
Output result:
3. print()
print() is used to output information about one or more strings or variable values. It can only print out the values of simple type variables, and cannot print arrays and objects (print () is slightly slower than echo())
4. print_r()
print_r() is a function used to print easy-to-understand information about variables.
print_r function prototype: bool print_r (mixed expression [, bool return] )
It can be seen from the above that the return value of print_r is Boolean, and the parameters are of mix type. Can be a string, integer, array, object class print_r() displays easy-to-understand information about a variable. If a string, integer, or float is given, the variable value itself is printed. If an array is given, the keys and elements will be displayed in a certain format. object is similar to an array.
<?php $a = array ('a' => 'apple', 'b' => 'banana', 'c' => array ('x','y','z')); print_r ($a); ?>
5. printf()
Function printf() is used to output formatted strings, and C language The functions with the same name are used the same way. The syntax format of the function is as follows:
printf(string $format[, mixed $args[, mixed $... ]])
Among them, $format is a required parameter, which is used to set the string and how to format the variables in it; the remaining parameters (such as $args) are optional parameters. Used to set the parameters inserted into $format at the corresponding "%" symbol.
The conversion format used by the first parameter of the printf() function is to replace the uncertain (dynamic) part of the string with a placeholder. The placeholder is converted from the percent symbol "%" to Represented by characters, as shown in the table below.
Format | Function Description |
---|---|
%% | Return the percent symbol |
%b | Binary number |
%c | Character corresponding to the ASCII value |
%d | Decimal numbers containing signs (negative numbers, 0, positive numbers) |
%e | Use lowercase scientific notation method (e.g. 1.5e 3) |
Use uppercase scientific notation (e.g. 1.2E 2) | |
Unsigned decimal number | |
Floating point number (local setting) | |
Floating point number (non-native setting) | |
Shorter %e and %f | |
Shorter %E and %f | |
Octal number | |
String | |
Hexadecimal number (lowercase letters) | |
Hexadecimal number (uppercase letters) |
The above is the detailed content of What are the commonly used methods for printing variables in PHP?. For more information, please follow other related articles on the PHP Chinese website!