PHP Echo Array with Example

The PHP echo array topic is best for beginners in PHP programming and anyone who wants to learn the basics of output display in a program.

However, if you are already a master in the field of programming, you can use some of the complex examples below.

These examples can help you express more about using the PHP echo function, especially when dealing with arrays.

Now, to know what is a PHP echo, it is a function that enables you to display certain outputs that you want.

This function can be equivalent to the print function but with some considerations to add.

To enlighten you on how the PHP echo function works, let us have example programs to test the function, specifically with arrays.

Use foreach Loop to echo an array of PHP

You may use the foreach loop with the PHP echo function to display the array in PHP.

To demonstrate how the foreach loop will work, it will iterate through each element of the array.

This method is the simplest form of fetching each element of the array in the example program.

But before we implement the method into the program, let us first know the correct syntax of the foreach loop.

Syntax:

foreach( $array as $variable ) {
// sample code
}

In each loop that goes through the array, each value in the array ($array) is assigned to the variable ($variable), and the pointer’s value is increased. 

Example:

<?php
$numbers = array("one","two","three","four","five","six","seven","eight");
echo "Array elements:\n";
foreach($numbers as $sample){
	echo "<pre>";
    echo $sample;
	echo "</pre>";
}
?>

Output:

Array elements:
one
two
three
four
five
six
seven
eight

Explanation of the Program

In the example above, each value of the array $numbers is assigned to the variable $sample.

Therefore, the variable $sample is then displayed using echo to display all the array elements of the array $numbers.

Use print_r() Function to echo array PHP

Another method to display or echo an array in PHP programming is through the use of the print_r() function.

The print_r() function is a built-in function of PHP and it is applicable for printing or displaying the values of a variable or values of an array.

This function prints all array values along with their index numbers.

Here’s the correct syntax of the print_r() function when we use it in a program.

Syntax:

print_r($variable, $boolean)

As shown in its syntax, the print_r() function takes two parameters, which are the $variable and the $boolean.

The “$variable” parameter is required since the program will print its value.

The $boolean parameter, on the other hand, is only optional with the default value of false.

This parameter only stores the output of the print_() function.

However, if the value of this parameter is set to true, then it will return the value that the program is supposed to print.

Example:

<?php
$sample = array("one","two","three","four","five","six","seven","eight");
echo "<pre>";
print_r($sample);
echo "</pre>";
?>

Output:

Array
(
    [0] => one
    [1] => two
    [2] => three
    [3] => four
    [4] => five
    [5] => six
    [6] => seven
    [7] => eight
)

Explanation of the program

In this example, we directly use the print_r() function instead of echo to display the values of the variable $sample.

This is possible because the variable $sample is passed as a parameter in the function.

Use var_dump() Function to echo array value in PHP

Another way to echo the print array in PHP is by using the var_dump() function.

To elaborate on the var_dump() function, it is applicable to display the values of an array.

This means that the function has the ability to print the details or values of any variable or argument.

See the syntax of the function below, so we can understand how to implement it in the program.

Syntax:

var_dump($variable)

As you can see, the function only takes a single parameter, which is the $variable.

This then returns the structured information of the $variable.

Example:

<?php
$sample = array("one","two","three","four","five","six","seven","eight");
echo "<pre>";
var_dump($sample);
echo "</pre>";
?>

Output:

array(8) {
  [0]=>
  string(3) "one"
  [1]=>
  string(3) "two"
  [2]=>
  string(5) "three"
  [3]=>
  string(4) "four"
  [4]=>
  string(4) "five"
  [5]=>
  string(3) "six"
  [6]=>
  string(5) "seven"
  [7]=>
  string(5) "eight"
}

Explanation of the program

Similar to the print_r() function, the var_dump() function prints the values of an array along their indexes.

It can also display the data type of each element as well as its length.

As a result, the function now can display the structured information and details of the elements in the variable or array.

Using var_export() function to echo an array in PHP

Through the use of the var_export() function, we can also be able to echo an array in PHP.

The var_export() function is able to display outputs or return a parsable string representation of a variable.

Programmers often use this function to print the structured values of an array in PHP just like the var_dump() function.

However, var_export() returns or shows a valid PHP code output.

Example:

<?php
$sample = array("one","two","three","four","five","six","seven","eight");
echo "<pre>";
var_export($sample);
echo "</pre>";
?>

Output:

array (
  0 => 'one',
  1 => 'two',
  2 => 'three',
  3 => 'four',
  4 => 'five',
  5 => 'six',
  6 => 'seven',
  7 => 'eight',
)

Code explanation

In this example, the var_export() function displays the values of the $sample array in a well-structured manner.

This may not be as structured as what the var_dump() function has been provided but the output of var_export() is valid to PHP code.

Using json_encode() function echo array PHP

Another awesome function to echo or display the values of an array in PHP is by using the json_encode() function.

The json_encode() function returns a string that shows how a value is written in JSON.

It is applicable to encode an array in PHP, making a JSON string that other people can read.

Example:

<?php
$sample = array("one","two","three","four","five","six","seven","eight");
echo json_encode($sample);
?>

Output:

["one","two","three","four","five","six","seven","eight"]

Code explanation

In this example, the json_encode() function displays exactly how the elements of $sample was declared.

The function read the elements as arrays and displays them in an array data type form along with the echo function.

Using foreach construct to echo array value in PHP

The foreach loop in the PHP construct is the easiest way to go through an array and echo or show its parts.

To explain the statement above, the foreach construct is able to show the elements of arrays and objects.

Moreover, a list of entities is moved through by using the foreach loop.

The way the loop is done is streamlined, so it doesn’t take too long to finish.

Take a look at the example below to clarify the function of foreach construct.

Example:

<?php
$samples = array("one","two","three","four","five","six","seven","eight");
foreach($samples as $sample){
	echo "<pre>";
    echo $sample;
	echo "</pre>";
}
?>

Output:

one
two
three
four
five
six
seven
eight

Code explanation

The example shows how the program executes to display the array of elements using the foreach construct.

The elements of the $samples variable was passed into the $sample to contain the elements and display them using the echo function.

Take Note: The whitespace is not kept in the DOM. Instead, the browser only shows a single space.

The solution is to put the results of the functions above inside a <pre></pre> tag.

Frequently Ask Question

How to echo an array in PHP?

To echo an array in PHP, you may use the various ways discussed above with their examples to guide you through the process.

How to echo JSON array in PHP?

The json_encode() method returns a string indicating how a given value is encoded in JSON.

It can be useful to encode an array in PHP, producing a string of readable JSON.

How do you echo an array value?

To echo an array, use the format echo $Array[0].

The array is the name of your array, and 0 is the index or key when echoing an associative array.

You can also substitute @ or * for an index to print the entire array.

Why echo is used in PHP?

Echo is used in PHP because it can print statements of strings and other data types with the proper declarations.

Echo is the simplest way of displaying objects in PHP programming.

How do I traverse an array in PHP?

There are a lot of ways to traverse or echo an array in PHP and you can have the discussions above as your reference.

What does Print_r do in PHP?

The PHP function print r() is applicable for printing or displaying the values of a variable or an array.

This function outputs all array values along with their corresponding index numbers.

What is the difference between Print_r and echo in PHP?

Both print_r and echo can display elements or values of a variable or any data type.

There is just a difference when dealing with other complex data types like arrays and lists and the print_r function can handle them better.

Is PHP array dynamic?

Yes, PHP arrays are dynamic in the sense that one can create an array, add new elements, delete elements, etc., without ever having to consider its size.

This means that an array can automatically expand or contract as needed.

More About Array

Conclusion

In conclusion, the topic has discussed the different ways of PHP echo array by revealing alternative functions.

These functions can be used to display array values as your output.

Moreover, the overall topic allows you to express your skills in programming by using the different ideas presented above.

By learning the discussed functions, you will have the ability to deal with different kinds of arrays without encountering unwanted errors.

Leave a Comment