Introduction
When developing applications in PHP, developers often need to inspect variables to understand their structure and values. This process is called debugging. PHP provides several built-in functions that help developers examine variables during development.
Two of the most commonly used debugging functions in PHP are:
- print_r()
- var_dump()
Both functions are used to display the contents of variables, especially arrays and objects. They help programmers understand what data is stored inside a variable and how it is structured.
What is print_r() in PHP?
The print_r() function is used to display human-readable information about a variable. It is commonly used to print the contents of arrays and objects in an easy-to-read format.
This function is very useful for debugging because it shows the structure and values of variables clearly.
Syntax
print_r($variable);
Parameters
- $variable – The variable you want to display.
Example of print_r()
Let’s look at a simple example using an array.
$students = array("Rahul", "Aman", "Neha", "Riya");
print_r($students);
Output
Explanation
Here:
- The variable $students is an array.
- The print_r() function prints the structure of the array.
- It shows both the index and the values stored in the array.
Using print_r() with Associative Arrays
$student = array(
"name" => "Ramesh",
"course" => "B.Tech",
"age" => 21
);
print_r($student);
Output
This makes it very easy to understand the key-value structure of associative arrays.
What is var_dump() in PHP?
The var_dump() function is another debugging function used to display information about variables.
However, var_dump() provides more detailed information compared to print_r().
It displays:
- Data type of the variable
- Length of the value
- Actual value stored in the variable
Because of this detailed information, var_dump() is extremely helpful for deep debugging and testing.
Syntax of var_dump()
var_dump($variable);
Example of var_dump()
$name = "Rahul";
var_dump($name);
Output
The output shows:
- string → Data type
- (5) → Length of the string
- “Rahul” → Actual value
This makes var_dump() very helpful for checking variable types and sizes.
Example of var_dump() with Arrays
$students = array("Rahul", "Aman", "Neha");
var_dump($students);
Output
Explanation
Here, var_dump() shows:
- The array size
- The data type of each element
- The length of each string
This level of detail is very useful for debugging complex programs.
Difference Between print_r() and var_dump()
Although both functions are used for debugging, they provide different types of information.
| Feature | print_r() | var_dump() |
|---|---|---|
| Purpose | Displays variable information | Displays detailed variable information |
| Output Format | Simple and readable | Detailed and technical |
| Data Type Information | Not shown | Shown |
| Length of Data | Not shown | Shown |
| Best Use | Quick debugging | Deep debugging |
Example Comparing Both Functions
$data = array("name" => "Ravi", "age" => 25);
print_r($data);
echo "
";
var_dump($data);
Output:
print_r() Output
var_dump() Output
Explanation
- print_r() gives a simple structure of the array.
- var_dump() shows the data types and values in detail.
When Should You Use print_r()?
Use print_r() when:
- You want simple readable output
- You are debugging arrays
- You want to quickly inspect variable values
Example:
print_r($array);
It is commonly used during basic debugging tasks.
When Should You Use var_dump()?
Use var_dump() when:
- want compleYou te information about a variable
- You need to check data types
- You want to debug complex variables
Example:
var_dump($variable);
It is very useful when dealing with:
- Objects
- Multi-dimensional arrays
- API responses
- Database results
Real Life Example
Suppose you are retrieving data from a database.
$user = array(
"id" => 101,
"name" => "Rohit",
"email" => "rohit@gmail.com"
);
print_r($user);
This will show the user data clearly.
But if you want to check the data types returned from the database, you should use:
var_dump($user);
This helps identify issues such as strings returned instead of integers.
Conclusion
Both print_r() and var_dump() are powerful debugging functions in PHP that help developers inspect variables and understand their structure.
The print_r() function is mainly used for displaying variables in a simple and readable format, especially when working with arrays. On the other hand, var_dump() provides more detailed information, including the data type and length of variables, which makes it extremely useful for deeper debugging.
print_r() vs var_dump() – Objective Questions (MCQs)
Q1. Which function provides more detailed type and value information?
Q2. Which function is better for debugging arrays and objects?
Q3. Which function returns output as string if second parameter is true?
Q4. print_r() output is ______.
Q5. Which can show the length and data type of values?