Learn more about PHP when debugging the difference between echo print () Print_r () Var_dump () sharing

Source: Internet
Author: User

Echo

Outputs one or more values (strings), separated by commas between multiple values. Echo is a language structure (language construct), not a real function and therefore cannot be used as part of an expression.

Print ()

The function print () prints a value (its arguments) and returns True if the string is successfully displayed, otherwise false. Same as ECHO, but slower than echo.

Print_r ()

You can simply print strings and numbers, and if you give a string, integer, or float, the value of the variable itself will be printed. object is similar to an array. The array is displayed with the enclosed key and the list of merits, starting with an array. Remember that Print_r () moves the pointer of the array to the last edge. Use Reset () to get the pointer back to the beginning.

$arr = Array (' name ' = ' Bob ', ' age ' = ' + ', ' sex ' = ' man ');p Rint_r ($arr);

The output shape is as follows:

array{     [Name] = Bob,     [age] = [     sex] = man}

However, it is meaningless to use Print_r () to display Boolean values and null:

Such as:

Print_r (true);           Output 1print_r (false);          No output print_r (null);          No output

Print_r () Output Boolean and null results are meaningless, because all are printed "\ n". Therefore, using the Var_dump () function is more suitable for debugging.

Var_dump ()

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

Var_dump (TRUE); output bool (TRUE) Var_dump (false);   BOOL (FALSE) Var_dump (null);   BOOL (NULL) Var_dump (Array (' name ' = ' Bob ', ' age ' = ') '); Array (2) {    [' name '] + = string (3) ' Bob '    [' Age '] = Int (20)}

Var_export ()

Similar to Print_r and var_dump, but seldom used.

The difference between Var_dump and Print_r

Var_dump returns the type and value of the expression and Print_r only returns the result, which is easier to read than the debug code using Var_dump.

  The differences between Print_r (), Var_export (), Var_dump () are detailed below:

Output of a two-dimensional array:

$arr =array (' a ' + '        aa ', ' b ' = = ' bbb ', ' c ' = = ' CCC '),        array (' a ' + = ' ddd ', ' b ' = ' eee ', ' c ' = > ' FFF '),         array (' a ' = ' = ' GG ', ' b ' = ' hh '), echo "<pre>";p Rint_r ($arr); echo "</pre>"; echo "< Pre> "; Var_export ($arr); echo" </pre> "; echo" <pre> "; Var_dump ($arr); echo" </pre> ";

Output from Print_r ($arr)

Array (    [0] = = Array        (            [A] = AA            [b] = BBB            [c] = = CCC        )    [1] = = Array        (            [A] = DDD            [b] = eee            [c] = FFF        )    [2] = = Array        (            [a] = = GG            [b] = hh        ))

Output from Var_export ($arr)

Array (  0 = =   Array (    ' a ' = = ' AA ',    ' b ' = ' bbb ',    ' c ' = ' CCC ',  ),  1 =   Array (    ' a ' = = ' ddd ',    ' b ' = = ' eee ',    ' c ' = ' fff ',  ),  2 = =   Array (    ' a ' = ' GG ',    ' b ' = ' hh ',  ),

Output from Var_dump ($arr)

Array (size=3)  0 = =     Array (size=3)    ' a ' + = String ' AA ' (length=2)     ' b ' = = = String ' bbb ' (length =3)     ' c ' = = String ' CCC ' (length=3)  1 = =     Array (size=3)    ' a ' + = String ' ddd ' (length=3)  ' b ' = = String ' eee ' (length=3)   ' c ' + = String ' FFF ' (length=3)  2 =     Array (size=2)    ' a ' + s Tring ' GG ' (length=2)     ' b ' = = String ' hh ' (length=2)

A JSON-formatted output is given below:

$arr =array (' a ' + ' AA ', ' b ' = ' BBB ', ' c ' + = ' CCC '),           array (' a ' + = ' ddd ', ' B ' + ' eee ', ' c ' = = ') FFF '),           array (' a ' = ' = ' GG ', ' b ' = ' hh '), $arra =json_encode ($arr), echo "<pre>";p Rint_r ($arra); echo " </pre> "echo" <pre> "Var_export ($arra) echo" </pre> "; echo" <pre> "; Var_dump ($arra); echo" </pre> ";

Print_r ($arra) output

[{"A": "AA", "B": "BBB", "C": "CCC"},{"a": "DDD", "B": "Eee", "C": "FFF"},{"a": "GG", "B": "HH"}]

Var_export ($arra) output

' [{"A": "AA", "B": "BBB", "C": "CCC"},{"a": "DDD", "B": "Eee", "C": "FFF"},{"a": "GG", "B": "HH"}] '

Var_dump ($arra) output

String ' [{"A": "AA", "B": "BBB", "C": "CCC"},{"a": "DDD", "B": "Eee", "C": "FFF"},{"a": "GG", "B": "HH"}] ' (length=84)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.