Difference between echo, print(), var_dump() and print_r()

echo

sure Output multiple values at a time , multiple values are separated by commas. Echo Yes language structure (language construct) is not a real function, so it cannot be used as part of an expression. echo It is not a function (it is a language structure), so you do not have to use parentheses to indicate parameters, either single quotation marks or double quotation marks.

print()

Print() Print A value (its parameters), the print() function is slightly slower than echo ().

print_r()

You can simply print out strings and numbers, while arrays are displayed in the form of a list of enclosed keys and values, starting with Array. However, print_r()'s output of Boolean values and NULL results is meaningless, because both are printing " n". Therefore, the var_dump() function is more suitable for debugging.

Print easy to understand information about the variable. If string, integer or float is given, the variable value itself will be printed. If array is given, keys and elements will be displayed in a certain format. Object is similar to array. Remember, print_r() will move the pointer to the last side of the array. Use reset () to return the pointer to the beginning.

var_dump()

This function displays structural information about one or more expressions, including the type and value of the expression. The array expands the value recursively, showing its structure by indentation.

Judge the type and length of a variable, and output the value of the variable. If the variable has a value, the input is the value of the variable and return the data type. This function displays structural information about one or more expressions, including the type and value of the expression. The array expands the value recursively, showing its structure by indentation.

Difference and connection

Difference between echo and print

Common points: first, echo and print are not strictly functions, they are language structures; They can only output string, integer and int floating point data. Cannot print composite and resource data;

The difference is that echo can continuously output multiple variables, while Print can only output one variable at a time The value printed by print can be directly copied to a variable, such as $a=print "123";

Echo can't. It doesn't behave like a function, so it can't be used in the context of a function. When used, The echo () function is slightly faster than print ().

Print () is not a function (but a language structure), although it can be bracketed and has a return value; So you can not use parentheses to enclose the parameter list. (See: http://docs.php.net/manual/zh/function.print.php

 print("Hello?World"); print? "print()?also?works?without?parentheses.";

Differences between var_dump() and print_r()

Common point: both can print compound variables such as arrays and objects.

Difference: print_r() can only print some information that is easy to understand, and print_r() will print the array Move the pointer of the array to the end Edge, use reset() to return the pointer to the beginning. While var_dump() can print not only data of composite type, but also variables of resource type. The output information of var_dump() is more detailed, which is easier to read than using var_dump for debugging code.

Example

(1) 4 ways to output string or number

 $str="string"; ?? ? echo $str; ? ? ? ? ?      //string print ($str);              //string ?? ? print_r($str);       //string ?? ? var_dump($str);     ?// string 'string' (length=6) ??? $ str=12; ? ?? ? echo $str;         //12 ?? ? print ($str);           //12 ?? ? print_r($str);       //12 ?? ? var_dump($str);?     // int 12

(2) 4 ways to output one-dimensional array

 $wosn_net = array( "username"=>"wosn", "age" ?? => "18" ); echo $wosn_net; ? ? ? ? ? ? // error:Array to string conversion...? ?Array print $wosn_net; ? ? ? ? ? ? // error:Array to string conversion...? ?Array print_r($wosn_net); ? ? ? ? // Array ( [username] => wosn [age] => 18 )  var_dump($wosn_net); ? ? ?// array (size=2) ? ?  ? ?  ? ?  ? ?  ? ?  // ' username' => string 'wosn' (length=4) ? ?  ? ?  ? ?  ? ?  ? ?  //' age' => string '18' (length=2)

(3) 4 ways to output multidimensional array

 $wosn_net = array( array("username"=>"wosn","age" ? ? ? => "18"), array("username"=>"wosnet","age" ? ? ?=> "19") ); echo $wosn_net; ? ? ? ? ? ? //error:Array to string conversion ....Array print($wosn_net); ? ? ? ? ?// error:Array to string conversion ....Array print_r($wosn_net); ? ? ? // Array ( [0] => Array ( [username] => wosn [age] => 18 )  ?? ? ?  ? ? ?  ? ? ?  ? ? // [1] => Array ( [username] => wosnet [age] => 19 ) )  var_dump($wosn_net); //The return value is as follows: //array (size=2) //  0 =>  //    array (size=2) //      'username' => string 'wosn' (length=4) //      'age' => string '18' (length=2) //  1 =>  //    array (size=2) //      'username' => string 'wosnet' (length=6) //      'age' => string '19' (length=2)

 

Extension

printf : function to format the text and output it, such as:

 $name="hunte"; $age=25; printf("my name is %s, age %d", $name, $age); %%- Return percentage symbol %B - Binary number %C - Characters according to ASCII values %D - Signed decimal number %E - Continuable 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 letters)

sprintf : It is similar to printf, but does not print. It returns formatted text. The others are the same as printf.

 Watson Blog
  • This article is written by Published on October 19, 2017 00:15:22
  • This article is collected and sorted by the website of Mutual Benefit, and the email address for problem feedback is: wosnnet@foxmail.com , please keep the link of this article for reprinting: https://wosn.net/689.html

Comment