
- PHP 7 Tutorial
- PHP 7 - Home
- PHP 7 - Introduction
- PHP 7 - Performance
- PHP 7 - Environment Setup
- PHP 7 - Scalar Type Declarations
- PHP 7 - Return Type Declarations
- PHP 7 - Null Coalescing Operator
- PHP 7 - Spaceship Operator
- PHP 7 - Constant Arrays
- PHP 7 - Anonymous Classes
- PHP 7 - Closure::call()
- PHP 7 - Filtered unserialize()
- PHP 7 - IntlChar
- PHP 7 - CSPRNG
- PHP 7 - Expectations
- PHP 7 - use Statement
- PHP 7 - Error Handling
- PHP 7 - Integer Division
- PHP 7 - Session Options
- PHP 7 - Deprecated Features
- PHP 7 - Removed Extensions & SAPIs
- PHP 7 Useful Resources
- PHP 7 - Quick Guide
- PHP 7 - Useful Resources
- PHP 7 - Discussion
Display array structure and values in PHP 7
An array in PHP is a type of data structure that can store multiple elements of similar data type under a single variable.
To display array structure and values in PHP, we can use two functions. We can use var_dump() or print_r() to display the values of an array in human-readable format or to see the output value of the program array.
Difference between print_r and var_dump
print_r: It is used to display the variable information in a human-readable format. Array values will be present in a format so that keys and elements can show. print_r also shows protected and private properties of objects but it will not show the static class and members.
Example
<?php $x = array ('x' => 'Dept', 'y' => 'Employee', 'z' => array ('a', 'b', 'c')); print_r ($x); ?>
Output
Output for the above print_r program will be:
Array ( [x] => Dept [y] => Employee [z] => Array ( [0] => a [1] => b [2] => c ) )
var_dump: It is used to display the structure information of one or more variables and expressions including its type and value. Arrays and objects are explored recursively with their values indented to show the structure.
Example
<?php $x = array(1, 2,3, array("x", "y", "z","a")); var_dump($x); ?>
Output
Output for the above var_dump program will be −
array(4) { [0]=> int(1) [1]=> int(2) [2]=> int(3) [3]=> array(4) { [0]=> string(1) "x" [1]=> string(1) "y" [2]=> string(1) "z" [3]=> string(1) "a" } }
Program using print_r and var_dump statement
Example
<?php $students = array("Rohan", "Mohan", "Thomas"); // it will print the students print_r($students); //echo "<hr>"; var_dump($students); ?>
Output
The output for the above program will be −
Array ( [0] => Rohan [1] => Mohan [2] => Thomas ) array(3) { [0]=> string(5) "Rohan" [1]=> string(5) "Mohan" [2]=> string(6) "Thomas" }
- Related Articles
- How to display array values with do while or for statement in my PHP?
- PHP How to display array values with text “even” to be displayed for even index values
- How can I count true and false values in my PHP array?
- PHP Pushing values into an associative array?
- Exceptions and Error in PHP 7
- Merging duplicate values into multi-dimensional array in PHP
- How to remove a character (‘’) in string array and display result in one string php?
- Difference Between Array and Structure
- Difference between Structure and Array in C
- C++ Program to Store and Display Information Using Structure
- Ignore null values in MySQL and display rest of the values
- Preg_replace_callback_array() in PHP 7
- JavaScript: take every nth Element of Array and display a fixed number of values?
- Array Doubling in Data Structure
- Migrating PHP 5.x to PHP 7 on CentOS 7
