How to print the contents of an array using PHP

H

How Can We Help?

How to print the contents of an array using PHP

//Array example
$arr = array(‘one’,’two’,’three’,’four’);

Use the print_r function.

Example: print_r($arr);

The example above will print out the following:
Array ( [0] => one [1] => two [2] => three [3] => four )

NOTE: If you want to have the contents of the array displayed in a more readable format, add a echo ‘<pre>’; statement before calling the print_r function.

Example of the results:

Array
(
    [0] => one
    [1] => two
    [2] => three
    [3] => four
)

Explanation:

print_r – Prints human-readable information about a variable (PHP 4, PHP 5)

About the author

Ian Carnaghan

I am a software developer and online educator who likes to keep up with all the latest in technology. I also manage cloud infrastructure, continuous monitoring, DevOps processes, security, and continuous integration and deployment.

About Author

Ian Carnaghan

I am a software developer and online educator who likes to keep up with all the latest in technology. I also manage cloud infrastructure, continuous monitoring, DevOps processes, security, and continuous integration and deployment.

Follow Me