


Detailed introduction to the differences between echo print() print_r() var_dump() in PHP during debugging
echo
Output one or more values (String), separated by commas. echo is a language construct, not a real function, so it cannot be used as part of an expression.
print()
The function print() prints a value (its parameter) if the string is successful Returns true if displayed, false otherwise. Same as echo, but slower than echo.
print_r()
You can simply print out strings and numbers. If a string, integer or float is given, it will Print the variable value itself. object is similar to an array. Arrays, on the other hand, are displayed as a bracketed list of keys and values, starting with Array. Remember, print_r() will move the array pointer to the end. Use reset() to return the pointer to the beginning.
$arr = array('name'=> 'bob','age' => 20, 'sex' => 'man'); print_r($arr);
The output is in the form:
Array{ [name] => bob, [age] => 20, [sex] => man }
But yes, it is meaningless to use print_r() to display Boolean values and NULL:
For example:
print_r(true); //输出1 print_r(false); //没有输出 print_r(null); //没有输出
The results of print_r() outputting Boolean values and NULL are meaningless, because they all print "\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 variable value and returns data type. This function displays structural information about one or more expressions, including the expression's type and value. Arrays will recursively expand values, showing their structure via indentation.
var_dump(true); //输出 bool(true) var_dump(false); // bool(false) var_dump(null); // bool(null) var_dump(array('name' => 'bob', 'age' => 20)); array(2) { ['name'] => string(3) 'bob' ['age'] => int(20) }
var_export()
Similar to print_r and var_dump, but rarely used.
The difference between var_dump and print_r
Var_dump returns the type and value of the expression while print_r only returns the result. Compared with debugging code, using var_dump is more convenient. Easy to read.
The differences between print_r(), var_export() and var_dump() are as follows:
Output of two-dimensional array :
$arr=array( array('a'=>'aa','b'=>'bbb','c'=>'ccc'), array('a'=>'ddd','b'=>'eee','c'=>'fff'), array('a'=>'gg','b'=>'hh') ); echo "<pre class="brush:php;toolbar:false">"; print_r($arr);echo "";echo "
"; var_export($arr);echo "";echo "
"; var_dump($arr);echo "";
The output result of 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 ) )
The output result of 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', ), )
The output result of 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' => string 'gg' (length=2) 'b' => string 'hh' (length=2)
The following is another output in json format:
$arr=array(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 class="brush:php;toolbar:false">"; print_r($arra);echo "";echo "
"; var_export($arra)echo "";echo "
"; var_dump($arra);echo "";
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)
The above is the detailed content of Detailed introduction to the differences between echo print() print_r() var_dump() in PHP during debugging. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
