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 Courses
php Courses
php Courses
Using print_r() and var_dump() to Print Arrays in PHP

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)
}
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendDebuggingPHPArrayprint_rvar_dump
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.