Using print_r() and var_dump() to Print Arrays in PHP
This article explains the two primary PHP functions for displaying arrays—print_r() for readable output and var_dump() for detailed type and value information—provides syntax examples, and shows the resulting output of each function.
PHP provides two main functions for printing arrays: print_r() and var_dump().
The print_r() function prints a variable in a human‑readable format, making the output easier to understand.
The var_dump() function outputs detailed information about a variable, including its type and value. When applied to arrays, it recursively expands the values and displays the structure with indentation.
print_r() Function
Example:
<?php
$arr_test = array(1, 2, 3);
print_r($arr_test);
?>Output of the example:
Array
(
[0] => 1
[1] => 2
[2] => 3
)var_dump() Function
Example:
<?php
$arr_test = array(1, 2, 3);
var_dump($arr_test);
?>Output of the example:
array(3) {
[0]=> int(1)
[1]=> int(2)
[2]=> int(3)
}Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
php Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
