Open In App
Related Articles

How to print all the values of an array in PHP ?

Improve Article
Improve
Save Article
Save
Like Article
Like

We have given an array containing some array elements and the task is to print all the values of an array arr in PHP. In order to do this task, we have the following approaches in PHP:

Approach 1: Using foreach loop: The foreach loop is used to iterate the array elements. The foreach loop though iterates over an array of elements, the execution is simplified and finishes the loop.

Syntax:

foreach( $array as $element ) {
    // PHP Code to be executed
}

Example:

PHP




<?php
// PHP program to print all 
// the values of an array
    
// given array
$array = array("Geek1", "Geek2",
           "Geek3", "1", "2","3");  
  
// Loop through array
foreach($array as $item){
    echo $item . "\n";
}
  
?>


Output

Geek1
Geek2
Geek3
1
2
3
 

Approach 2: Using count() function and for loop: The count() function is used to count the number of element in an array and for loop is used to iterate over the array.

Syntax:

for (initialization; test condition; increment/decrement) {
    // Code to be executed
}

Example:

PHP




<?php
// PHP program to print all 
// the values of an array
    
// given array
$array = array("Geek1", "Geek2",
            "Geek3", "1", "2","3");  
  
$items = count($array);
  
// Loop through array
for($num = 0; $num < $items; $num += 1){
    echo  $array[$num]. "\n";
}
  
?>


Output

Geek1
Geek2
Geek3
1
2
3
 

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 01 Jun, 2020
Like Article
Save Article
Previous
Next
Similar Reads
Complete Tutorials