Home > Article > Backend Development > Introduction to the usage of echo, print, print_r, printf, sprintf, var_dump in php_PHP tutorial
The article introduces echo, print, print_r, printf, sprintf, and var_dump. Friends who need to know more can refer to it.
1. echo
Definition and Usage
The PHP echo() function outputs one or more strings.
echo "" This method is also possible, no parentheses are needed
Grammar
echo(strings)
Parameter Description
strings required. One or more strings to send to the output.
Tips and Notes
Note: echo() is not actually a function, so you don't need to use parentheses with it. However, if you wish to pass one or more arguments to echo(), then using parentheses will cause a parsing error.
Tip: The echo() function is a little faster than the print() function.
Tip: The echo() function can use simplified syntax. See example 5.
The code is as follows | Copy code | ||||||||
Example 1
Output:
Who's John Adam? | |||||||||
代码如下 | 复制代码 | ||||||||
$a="55nav"; |
Roses are =$color?>
2. print Print() has the same usage as echo(), but echo is slightly faster than print. It's actually not a function either, so you don't need to use parentheses on it. However, if you wish to pass more than one argument to print(), a parsing error will occur using parentheses. Note that print always returns 1, which is different from echo, that is, you can use print to assign values, but it has no practical meaning. Example:The code is as follows | Copy code |
$a = print("55nav"); // This is allowed <🎜> echo $a; // The value of $a is 1<🎜> ?> |
The code is as follows | Copy code |
$a="55nav";<🎜> $c = print_r($a); <🎜> echo $c; // The value of $c is TRUE <🎜> $c = print_r($a, ture); <🎜> echo $c; // The value of $c is the string 55nav <🎜> ?> |
4. printf function
The printf function returns a formatted string.
Syntax: printf(format,arg1,arg2,arg++)
The parameter format is the conversion format, starting with the percent sign ("%") and ending with the conversion character. The following are possible format values:
* %% – Returns the percent symbol
* %b – binary number
* %c – character
according to ASCII value
* %d – signed decimal number
* %e – Continuous counting method (such as 1.5e+3)
* %u – unsigned decimal number
* %f – floating point number (local settings aware)
* %F – floating point number (not local settings aware)
* %o – octal number
* %s – string
* %x – Hexadecimal number (lowercase letters)
* %X – hexadecimal number (uppercase letter)
Parameters such as arg1, arg2, arg++ will be inserted into the main string at the percent sign (%) symbol. The function is executed step by step, at the first % sign, arg1 is inserted, at the second % sign, arg2 is inserted, and so on. If there are more % symbols than arg arguments, you must use placeholders. The placeholder is inserted after the % sign and consists of a number followed by "$". You can use numbers to specify the displayed parameters. See the example for details.
Example:
The code is as follows | Copy code | ||||
?> |
%% - Returns the percent symbol
%b - binary number
%c - character
according to ASCII value
%d - signed decimal number%e - scientific notation (e.g. 1.5e+3)
%u - unsigned decimal number
%f - floating point number (local settings aware)
%F - floating point number (not local settings aware)
%o - octal number %s - string
%x - hexadecimal number (lowercase letters)
%X - hexadecimal number (uppercase letters)
arg1, arg2, ++ and other parameters will be inserted into the main string at the percent sign (%) symbol. This function is executed step by step. At the first % sign, arg1 is inserted, at the second % sign, arg2, and so on.
代码如下 | 复制代码 |
例子 输出: Hello world. Day number 123 例子 2 输出: 123.000000 例子 3 输出: With 2 decimals: 123.00 With no decimals: 123 |
The code is as follows | Copy code |
Example Example 1 $str = "Hello";<🎜> $number = 123;<🎜> $txt = sprintf("%s world. Day number %u",$str,$number);<🎜> echo $txt;<🎜> ?> Output: Hello world. Day number 123 Example 2 $number = 123;<🎜> $txt = sprintf("%f",$number);<🎜> echo $txt;<🎜> ?> Output: 123.000000 Example 3 With no decimals: %1$u",$number); echo $txt; ?> Output: With 2 decimals: 123.00 With no decimals: 123 |
PHP String Function
6. var_dump function
var_dump (PHP 3 >= 3.0.5, PHP 4, PHP 5)
var_dump -- Print information about variables
void var_dump ( mixed expression [, mixed expression [, ...]] )
This function displays structural information about one or more expressions, including the type and value of the expression. Arrays will expand values recursively, showing their structure through indentation.
Tip: To prevent the program from outputting the results directly to the browser, you can use output-control functions to capture the output of this function and save them to a variable of type string, for example.
You can compare var_dump() and print_r().
Example
The code is as follows
|
Copy code | ||||
<p align="left"> </p> <div style="display:none;"> <?php<span id="url" itemprop="url"> </span> $a = array (1, 2, array ("a", "b", "c"));<span id="indexUrl" itemprop="indexUrl"> </span> var_dump ($a);<span id="isOriginal" itemprop="isOriginal"> </span> /* Output: <span id="isBasedOnUrl" itemprop="isBasedOnUrl"> </span> array(3) {<span id="genre" itemprop="genre"> </span> [0]=><span id="description" itemprop="description"> </span>int(1)</div> [1]=> <div class="art_confoot">int(2)</div> [2]=> array(3) { [0]=> String(1) "a" [1]=> String(1) "b" [2]=> String(1) "c" } } */ $b = 3.1; $c = TRUE; var_dump($b,$c); /* Output: float(3.1) bool(true) */ ?>http://www.bkjia.com/PHPjc/629092.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/629092.htmlTechArticleThe article introduces echo, print, print_r, printf, sprintf, var_dump. Friends who need to know more can refer to it. 1. Echo definition and usage PHP echo() function outputs one or more strings...