PHP Array Print

For printing a specific value it is need to find the the index of the value. Every index refers to a specific value. If we notice above array then we see that, its an one dimensional array which size is seven. As array index is started from zero, so here array started from zero and ended at position six.


In order to print value from an array it is needed to mention the position of index every time. Here we’ve kept integer value in every indexes. But it is possible to store every types of value such as integer, double, string, bool in array index. Below there is an implementation of array with some integer values stored in $numbers variable…….


Suppose, we want to print 45 from declared array. If we notice then we find that 45 is stayed in $numbers array having index 2. So, for printing 45 we need use code as shown below.


Above code is used for printing a single value from an array. If anyone wants to print all values of an array then it is needed to take help of loop. Specially, for printing values of one dimensional array foreach loop is quiet popular. General loop such as for, while and do while is also used for printing the values of an array.

Other types of functions are also used for printing arrays. We will discuss about those in upcoming posts.

Actually, for printing the values of an array its size should be needed to know. Main concept of printing all the values is that every time we will put an index number and print a number. This process will continue until index reaches at last position. The loop will be terminated when index reaches at last position. Let’s see the below code for printing all values of an array by using for loop.


We can also print all the values of an array by using while loop. The concept is all the same during using while loop. Here we used initialization at first by $index variable. Every time after printing a value we increased the value of $index by 1. This increase is mandatory for printing an array values to a fixed range. Without it program will be executed for unlimited time. As a result browser can be crashed.


If we want to print values of an array by less work then foreach can be the best option. Because, this foreach function runs until it prints last index value. Its using process is also easy. Let’s see below code for better understanding.

Playing with array is a fun. Its enjoyable also. Anyone can sharp his/her idea by practice with array and it several indexes. Hope that, we’ve succeeded to give a details idea about printing array values. For any query just comment.

Share This on Social Media