What is the output in php? What's the difference between these?
1. echo
echo is a language construct, that is, a keyword, not a real function, so it cannot be used as an expression. Partially used. You don't need to add parentheses when using it, you can just add it. Only basic types are supported, except for Boolean types. When echo true, it displays 1, and when echo false, nothing happens. Echo cannot output array type
<?php $arr = array(1,2,3,4,5); echo $arr; ?>
array, output array For Boolean type, true outputs 1 false outputs nothing
2. print()
Only one string can be output, and the syntax of comma-separated multiple display variables is not supported. Print can output array type data,
<?php $a = "php.cn"; $arr = array(1,2,3,4); print($a); echo "</br>"; print($arr); echo "</br>"; print($arr[1]); ?>
Note: $a is a string, which can be output using print $arr is an array, which cannot be output
$ arr is also an array. When using print to output, I added a subscript and output the first digit
3.print_r()
Okay Print out the value of complex type variables (such as arrays, objects)
<?php $arr = array( 'a' => 'apple', 'b' => 'banana', 'c' => array ('x','y','z')); print_r($arr); ?>
Note: If the variable is string, integer and float, its value will be output directly. If the variable is an array, a formatted value will be output. The following array is easy to read,
4.printf();
Syntax: printf(format,arg1,arg2,arg++)
The format parameter is the conversion format, starting with the percent sign ("%") and ending with the conversion character. The following are possible format values:
* %% – Returns the percent sign
* %b – Binary number
* %c – Character according to the 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 – Float Points (not local settings aware)
* %o – Octal number
* %s – String
* %x – Hexadecimal number (lowercase letters)
* %X – Sixteen Parameters such as base numbers (uppercase letters)
arg1, arg2, arg++, etc. 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. Placeholders are inserted after the % sign and consist of numbers and "\$". You can use numbers to specify the displayed parameters
<?php header("Content-type: text/html; charset=utf-8");//设置编码 printf("My name is %s %s。","55nav", "com"); // My name is 55nav com。 printf("My name is %1$s %1$s","55nav", "com"); // 在s前添加1$或2$.....表示后面的参数显示的位置,此行输出 My name is 55nav 55nav因为只显示第一个参数两次。 printf("My name is %2$s %1$s","55nav", "com"); // My name is com 55nav ?>
Note: This is still relatively rarely used in php
5. var_dump function
Function: Output the content, type of the variable or the content, type, and length of the string. Commonly used for debugging.
<?php $a = 1 ; $b = "123"; var_dump($a,$b); ?>
Note: The type of output $b is a string type