How to print or echo all the values of an array in PHP?
Answer: Using foreach
loop
We can print all the values of the array using a PHP foreach
loop. The foreach
loop iterates through each element within the array.
The foreach
loop works on an array and objects only. We will get an error if we try to use the foreach
loop with variables or other data types.
It is the easiest method to print all the array elements at once.
Example: Using foreach
loop
In the given example, we have used the foreach
loop to print all the elements of an array.
<!DOCTYPE html>
<html>
<head>
<title>Print all the values of array</title>
</head>
<body>
<?php
$fruits = array("Apple", "Mango", "Banana", "Pears", "Watermelon", "Guava");
echo "The array is: <br>";
foreach($fruits as $fruits){
echo $fruits . "<br>";
}
?>
</body>
</html>
Output
The array is:
Apple
Mango
Banana
Pears
Watermelon
Guava
Using print_r()
function
We can also print all the values of the array using the print_r()
function. This function is basically used to print the values in more human-readable form.
The print_r()
function takes two arguments, the first parameter is the variable name whose value we want to print using this function. The second parameter is the return type which is optional and by default its value is false.
This function prints all the values of the array along with their index value.
Example: Using print_r()
function
In the given example, we have used the print_r()
function to print all the values of the array.
<!DOCTYPE html>
<html>
<head>
<title>Print all the values of array</title>
</head>
<body>
<?php
$fruits = array("Apple", "Mango", "Banana", "Pears", "Watermelon", "Guava");
print_r($fruits);
?>
</body>
</html>
Output
Array ( [0] => Apple [1] => Mango [2] => Banana [3] => Pears [4] => Watermelon [5] => Guava )
Using var_dump()
function
The var_dump()
function is used to print all the values of the array at once. The function prints the value of the array along with its index value, data type, and length.
This function takes the single parameter which can be the name of the variable whose value we want to print.
Example: Using var_dump() function
In this example, we have used the var_dump()
function to print all the values of an array. This method also prints the length, index value, and the datatype of the value.
<!DOCTYPE html>
<html>
<head>
<title>Print all the values of array</title>
</head>
<body>
<?php
$fruits = array("Apple", "Mango", "Banana", "Pears", "Watermelon", "Guava");
var_dump($fruits);
?>
</body>
</html>
Output
array(6) { [0]=> string(5) "Apple" [1]=> string(5) "Mango" [2]=> string(6) "Banana" [3]=> string(5) "Pears" [4]=> string(10) "Watermelon" [5]=> string(5) "Guava" }
Conclusion
In this lesson, we have learned how to print all the values of an array in PHP. There are several methods and pre-defined functions that enable us to print all the values of the array at once. Here, we have discussed three methods with the help of them we can print all the values of an array, these methods are:
- Using
foreach
loop
- Using
print_r()
function
- Using
var_dump()
function