2 ways of printing an array in php

2 ways of printing an array in php

We use echo statement in PHP to output the value of an expression like a single variable. While echo is much more useful, but it’s have some limitation to print array contents. For example, while debugging a script, we needs to see not just the value of an expression, but also its type and it’s index information. So, this type of problem occur with echo while we are trying to deal with composite data types like arrays and objects.

To solve this problem, PHP provides two very useful functions that can be used to output a variable’s value recursively called print_r()and var_dump().


– Create an array:

$my_array = array("elem_1","elem_2","elem_3");


– Using Print_r() function: Print_r can return its output as a string, as opposed to writing it to the scripts standard output



– Using var_dump() function:  Var_dump()is capable of outputting the value of more than one variable at the same time. Var_dump() outputs the data types of each value.



Was this information useful? What other tips would you like to read about in the future? Share your comments, feedback and experiences with us by commenting below!

Share It

Related Posts