My understanding of the echo (), print (), print_r () function is that echo can input string variable constants, and print is similar to echo, however, print_r can print arrays, but the first two cannot,
My understanding of the echo (), print (), print_r () function is that echo can input string variable constants, and print is similar to echo, however, print_r can print arrays, but the first two are not. I will introduce the usage and differences of the three.
Echo is a PHP statement, print and print_r are functions, and the statement does not return values. the function can return values (even if it is not used)
Print () can only print values of simple type variables (such as int and string)
Print_r () can print values of complex types of variables (such as arrays and objects)
Echo outputs one or more strings
Echo-output one or more strings: Echo (string arg1 [, string...]). the returned value is null. the code is as follows: echo "Hello", "friend ";
Print-output a string:Int print (string arg). the returned value is an integer. the code is as follows: print "Hello Friend ";
You can perform the following operations:
- $ Name = print "nihao n ";
- $ Str = 'Test print value is '. $ name;
- Eval ("$ print =" $ str ";");
- Echo $ print;
Print_r-prints easy-to-understand information about variables.
Bool print_r (mixed expression [, bool return]) // The return value is Boolean and the parameter type is mix. it can be a string, an integer, an array, and an object class print_r () displays easy-to-understand information about a variable. If the value is string, integer, or float, the variable value is printed. If array is provided, keys and elements are displayed in a certain format. Objects are similar to arrays.
Print_r () will move the array pointer to the last edge. you can.
- Print_r (str );
- Print_r (int );
- Print_r (array );
- Print_r (obj );
The following are examples of the four methods that can output strings.
Echo, print (), printf (), print_r ()
EchoMultiple values can be output at a time. multiple values are separated by commas. echo is a language construct rather than a real function. Therefore, it cannot be used as part of an expression.
Syntax: echo "Hello", "World"; syntax error: echo ("Hello", "World ");
Print () function,Print () prints a value (its parameter). If the string is successfully displayed, true is returned. otherwise, false is returned. the code is as follows:
- If (! Print ("Hello, World ")){
- Die ("you are not listening to me ");
- }
Printf (),Printf () is derived from printf () in C language (). This function outputs formatted strings.
Syntax: printf (format, arg1, arg2, arg ++)
Format specifies the string and how to format the variable;
Parameters such as arg1, arg2, ++ are inserted to the percent sign (%) in the main string. This function is executed step by step. In the first % symbol, insert arg1, insert arg2 at the second % symbol, and so on. The code is as follows:
- $ Str = "Hello ";
- $ Number = 123;
- Printf ("% s world. Day number % u", $ str, $ number );
If the % symbol is greater than the arg parameter, you must use a placeholder. After the placeholder is inserted with the % symbol, it consists of numbers and "$". See the example code below:
- $ Number = 123;
- Printf ("With 2 decimals: % 1 $. 2fbr/> With no decimals: % 1 $ u", $ number );
Print_r () and var_dump ()
Print_r () can print strings and numbers, while the Array is displayed in the form of an enclosed key and a list of values, and starts with Array. the code is as follows:
- $ A = array ('name' => 'Fred ', 'age' => '15', 'wife' => 'wilm ');
- Print_r ($ );
- Output: Array
- {
- [Name] => Fred
- [Age] => 15
- [Wife] => Wilma
- }
The code is as follows:
- Class P {
- Var $ name = 'Nat ';
- //...
- }
- $ P = new P;
- Print_r ($ p );
- Output: Object
- {
- [Name] => nat
- }
But print_r () outputs Boolean values and NULL results are meaningless, because both print "n", so var_dump () function is more suitable for debugging.