Echo (), print (), print_r () usage in php. 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 allowed. below I use echo (), print (), print_r () in this function, echo can input a string variable constant. print is similar to echo, but print_r can print an array, and the first two are not allowed, let me introduce in detail the usage and difference 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
Descr resume ption
Void echo (string arg1 [, string...]) // the returned value is null.
The code is as follows: |
|
Echo "Hello", "friend ";
|
Print-output a string
Descr resume ption
Int print (string arg) // The return value is an integer.
The code is as follows: |
|
Print "your good friend ";
|
You can perform the following operations:
The code is as follows: |
|
$ 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 () moves 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:
Four methods can output strings. Echo
Print ()
Printf ()
Print_r ()
Echo
Multiple 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.
Correct syntax: echo "Hello", "World ";
Syntax error: echo ("Hello", "World ");
Print ()
The print () function prints a value (its parameter). If the string is successfully displayed, true is returned. otherwise, false is returned. For example,
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, at the second % symbol, insert arg2, and so on.
Example :? Php
The code is as follows: |
|
$ Str = "Hello "; $ Number = 123; Printf ("% s world. Day number % u", $ str, $ number ); ?> # Results ====== Hello world. Day number 123 |
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 Example 3.
Example :? Php
The code is as follows: |
|
$ Number = 123; Printf ("With 2 decimals: % 1 $. 2fbr/> With no decimals: % 1 $ u", $ number ); ?> # Result With 2 decimals: 123.00 With no decimals: 123 |
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. For example,
The code is as follows: |
|
$ A = array ('name' => 'Fred ', 'age' => '15', 'wife' => 'wilm '); Print_r ($ ); Output: Array { [Name] => Fred [Age] => 15 [Wife] => Wilma } |
The same is true for objects. For example,
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 ". Therefore, using the var_dump () function is more suitable for debugging.
The functions of extract (), print (), print_r () are interpreted as constants of string variables that can be input by echo. print is similar to echo, but print_r can print the array, and the first two are not. below I...