. Advertisement .
..3..
. Advertisement .
..4..
Echoing is one of the most basic operations that you need to familiarize yourself with while using PHP. However, it does have a variety of tricks that not many people know about.
One of them is how to echo array in PHP. This tip can help you tremendously when you need to take care of huge databases.
Echo Array In PHP With The foreach Loop
There is no denying that the foreach loop approach is the simplest out of all three solutions. After all, it iterates through every single element of your array, fetching each one in a manual way. While this may appear clunky, it rarely makes an error.
Here is the syntax:
foreach( $arrayName as $variableName ) {
// action
}
As you can easily see, each array value gets assigned to a corresponding variable. Then, it continues iterating over the entire array by incrementing with the pointer’s help.
<?php
$programming_language = array('JavaScript','PHP','Python','Pascal','Ruby');
echo "Here is the array:<br>";
foreach($programming_language as $programming_language){
echo $programming_language.'<br>';
}
?>
The output is as follows:
Here is the array:
JavaScript
PHP
Python
Pascal
Ruby
Echo Array In PHP With The print_r() function
print_r()
is an integrated function within PHP. Its main responsibility is to print any value that a variable stored. As such, we can make use of this nature to echo any array we want. You do need to keep in mind, however, that the printed array will have its index number accompanying them.
Here is its syntax: print_r($variableName, $boolVariable)
There are two parameters, the first one is mandatory, as it’s the one whose value you want to print. The second one has an optional nature, as you can ignore it, and it will default to false. As a result, the function won’t return the output.
Let’s return to the example above:
<?php
$programming_language = array('JavaScript','PHP','Python','Pascal','Ruby');
echo "Here is the array:<br>";
print_r($programming_language);
?>
As you can see, the programming_language array gets passed in as the first parameter. We don’t need the output for anything else so we won’t touch the second input.
The computer will then print out this output:
Here is the array:
Array ( [0] => JavaScript [1] => PHP [2] => Python [3] => Pascal [4] => Ruby )
This method serves as the basis for printing console in PHP.
Echo Array In PHP With The var_dump() function
The last method, var_dump()
, is not that popular due to its throughout nature. It differs greatly from the two solutions above in that it prints not only the values but also their index, data type, and even length.
Of course, in specific circumstances, this structured information feedback can help tremendously. Let’s take a look at its syntax:
var_dump($variableName)
You only need to deal with one single parameter, making it easier to manipulate.
<?php
$programming_language = array('JavaScript','PHP','Python','Pascal','Ruby');
echo "Here is the array:<br>";
var_dump($programming_language);
?>
The output is as follows:
Here is the array:
array(5) { [0]=> string(10) "JavaScript" [1]=> string(3) "PHP" [2]=> string(6) "Python" [3]=> string(6) "Pascal" [4]=> string(4) "Ruby" }
Conclusion
This guide has shown you all there is to know about the ways to echo array in PHP. We hope that you can now confidently approach this issue and solve it in a timely manner.
Remember that as long as you recognize which situation best fits with which solution, nothing can stop you.
Leave a comment