How to print an array in PHP? | print_r() function

How to print an array in PHP: As a developer ,you have to deal with arrays on a daily basis. Some of them are multidimensional and can be a nightmare whereas some are straight forward and simple that contain different data types. Printing an Array in PHP is easy by using print_r() functions and by following the below tutorials.

A group of similar product that are of same size and type is contained by array in PHP computer programming. This array can also contain character, integers and anything else which has a defined data type.

This helps in human readable form with the help of print_rPHP function. This is shown as print_r($your_array).

Below is an example which will show an array is defined and printed. <pre> is a tag which indicates a code in preformatted text. Displaying in a fixed width font is the cause of the text. It helps in preventing line spaces and line break which helps the human observer to read.

How to print an array in PHP

Example 1:



$Names = array ('a' =>'Niharika', 'b' =>'Megha', 'c' => Array ('Dolphe', 'Dolphee));

print_r ($Names);



Then you will run the code you will observe result look like this:







=> Array
     [0] =>Dolphe
     [1] =>Dolphee

Other Variations of Print_r

There is a second parameter to Print_r which is possible to store the result of the Print_r. From function they help in preventing any output. The function of print_rcan also be augmented withthe help ofvar_dumpand var-exportfor showing the private properties. It also shows protected the object; which includes typeand value. The main difference of the two is that  valid PHP codeis not returned back by varbut it can be done by var_export.

What is an Array?

It is a special variable which can even hold many value at a time. For example if you want to have list of items like list of watch brand name storing the watch in a single variables could look like

$watch1 = “titan”;

$watch2 = “fastrack”

$watch3 = “trimax”

Then if you want to loop through the watches and find specific one and what if there are not 3 watches you have 300 watches in your list

This solution can create an array!

An array can hold many names and value under a single frame and you can easily access the value by referring to an index number.

The main uses of PHP:

PHP(Hypertext Preprocessor) is known for its server-side language which is used to add enhanced features to a website developed in HTML such as shopping carts, CAPTCHA codes, surveys and login boxes. You can even build an online community, integrated Facebook with the help of the website. Moreover, you can also generate PDF files. With the help of PHP’S file handling functions you can even use the GD library and you can create photo gallery with PHP to generate thumb line images and resize, crop image and add water mark to the image.

If you are planning to host banner ads on your website you can even use rotator from PHP at random. If you want to rotate Quotations then you need to use same feature. If you want to set page redirects you can even use PHP. If you want to know how often visitors checkout your website you can use PHP to set up a counter.

Method 1:

Printing array in PHP is one the most necessary and basic function. Fellow are some of the tricks which will make this process easy. 

  1. ‘print_r’ use it in a better way.

This command ‘print_r’ prints the human readable information for a variable and its quite useful for arrays. You can use this command as below:

$names = array('megha', 'Dolphee');


And the browser will give you a result:

Array([0] =>megha [1] =>Dolphee)
  • ‘<pre>’ is a tags that can make it more readable.

To get more readability output, like this:


print_r( $names );


will come up with this results:



 [0] =>Megha

[1] =>Dolphee

  • To save time write a custom function:

If you are interested in working in large project, you might consider writing a custom function and auto-loading. You can also include it onto every page:

functionprint_rx($arr) {

echo"<pre>"; print_r($arr); echo "</pre>";


Method 2:  using again print_r and using Var dump

  1. Using print_r

If you are using this function for the first time you can badly-formatt and mess of characters on your screen. One tip if you can use priny_r inside a <pre> tag, you will also get a much nicer display:


$myArray = array(1, 2, 3, 'example' => 7);




If u snippet above you’ll see many array in printing out on to the screen and proper spacing and newslines are bing used:



   [0] => 1

  [1] => 2

[2] => 3

  • Var_dump

To find out the element is a string or an integer but if you want to see data type which are in array then. Here this function var_dump is come in handle:

 <?php //A simple PHP array. $myArray = array(1, 2, 3, ‘example’ => 7); //Print it out with var_dump. var_dump($myArray);

This format will look like this after a format output of the array:

Array (size=4)

0 =>int 1

1 =>int 2

2 =>int 3

 ‘Example’ =>int 7

This this structure gives more information of the structure of array and tells us what type of variables they are.

Benefits of PHP

  • You can use PHP to rotate them at random.
  • PHP helps in easy to set up page redirects using.
  • You can even use PHP in handling function like creating photo galleries and editing them.


Printing an array in PHP is an easy job if you follow the rules, tags, and instructions given above. There are many ways to print the array in PHP it depends on you what you like to follow we have presented you the 2 easiest which can help you in doing this process.

Share this easy print array function on your code boards and learn other programming basics about How to print an array in PHP, visit CtrlProgram tutorials.

Leave a Reply

Your email address will not be published. Required fields are marked *