php

PHP print_r() Function

Two built-in functions exist in PHP to print the content of the variable. These are print_r() and var_dump(). The value of different types of variables is printed in human-readable format by using the print_r() function. If you want the value with the data type of the variable then the var_dump() function is used. The uses of the print_r() function in PHP have shown in this tutorial.

Syntax:

The print_r() can take two arguments. The first argument is mandatory and the second argument is optional. It returns a string value or Boolean value.

string | bool print_r(mixed $value, bool $return = false);

The first argument of this function takes any type of variable supported by PHP, such as integer, float, Boolean, string, array, and object.

If the value of the second argument of this function is set to True, then the return value of the print_r() function will be a string and can be stored into a variable, otherwise, the function returns True.

Different Uses of print_r() Function

The uses of the print_r() function for different types of variables have shown in this part of the tutorial by using multiple examples.

Example-1: Use of print_r() function for primitive data

The print_r() function is mainly used to print the compound data like array or object but it can be used to print the primitive data like string, number, Boolean, and floating-point. Create a PHP file with the following script to check the uses of the print_r() function for printing primitive data.

<?php  

    //Initialize a string value
    $value = "PHP Programming";  
    echo "The print_r() output of string value is: ";
    print_r($value);  
    echo "</br>";  

    //Initialize an integer value
    $value = 6800;  
    echo "The print_r() output of integer value is: ";
    print_r($value);  
    echo "</br>";  

    //Initialize an boolean value
    $value = True;  
    echo "The print_r() output of boolean value is: ";
    print_r($value);  
    echo "</br>";  
     
    //Initialize an float value
    $value = 4.5;  
    echo "The print_r() output of float value is: ";
    print_r($value);  
    echo "</br>";  
   
?>

Output:

The following output will appear after executing the above script.

Example-2: Use of print_r() function to print numeric array

Create a PHP file with the following script to check the uses of the print_r() function for printing a numeric array of 6 elements. Using the print_r() function is an efficient way to check the content of an array for debugging purposes. The following script will print the structure of the array with the index and the corresponding value.

<?php  

    //Declare a numeric array  
    $languageArr = ["PHP", "Java", "C++", "Bash", "C#", "Python"];

    //Print the values of the array
    echo "The print_r() output of the numeric array: <br/>";
    print_r($languageArr);  
    echo "<br/>";
   
?>

Output:

The following output will appear after executing the above script.

Example-3: Use of print_r() function to print associative array

Create a PHP file with the following script to check the uses of the print_r() function for printing an associative array of 4 elements. The following script will print the structure of the associative array with the key and the corresponding value like the previous example.

<?php
$assoc_arr = array('0111897' => 3.23, '0111345' => 3.78, '0111236' => 3.45, '0111762' => 2.99);

//Print the values of the array
echo "The print_r() output of the associative array: <br/>";
print_r($assoc_arr);  
echo "<br/>";
?>

Output:

The following output will appear after executing the above script.

Example-4: Use of print_r() function to print object variable

Create a PHP file with the following script to check the uses of the print_r() function for printing an object of a class. A class named Employee has declared in the script that contains three public variables and a constructor to initialize the variables. An object of the class has been declared in the script and the structure of this object has been printed by using the print_r() function. The output of the print_r() for the object variable will be looked like the output of the associative array.

<?php
//Define a class
class Employee
{
    public $name;
    public $dept;
    public $post;

    public function __construct($n, $d, $p)
    {
        //Initialize the class variables
        $this->name = $n;
        $this->dept = $d;
        $this->post = $p;
    }
}

//Declare an object of the class
$objEmp = new Employee("Ali Hamza", "HR", "Manager");
//Print the values of the object properties
echo "The print_r() output of the object: <br/>";
print_r($objEmp);  
echo "<br/>";

Output:

The following output will appear after executing the above script. According to the output, the property name of the object has printed like the array key and the property value of the object has printed like the array value.

Example-5: Store the output of the print_r() function into a variable

The second argument of the print_r() function is required to use to store the output of the print_r() function into a variable. Create a PHP file with the following script to know the way of storing the output of the print_r() function into the variable. The print_r() function has been used two times in the script without and with the second argument. The second argument value of the print_r() function has been set to True for storing the output of the print_r() function into a variable.

<?php  

    //Declare a numeric array  
    $subjectArr = array("CSE-202", "CSE-305", "CSE-401", "CSE-407");

    echo "The output of the variable: <br/>";
    //Store the values into a variable
    $arr = print_r($subjectArr);

    //Print the value of the variable
    echo $arr;

     echo "<br/>The output of the variable with the second argument: <br/>";
    //Store the values into a variable
    $arr = print_r($subjectArr, true);

    //Print the value of the variable
    echo $arr;
   
?>

Output:

The following output will appear after executing the above script. According to the output, the print_r() function returns 1 with the array structure when the second argument didn’t use and returns the array structure only when the true value has been used in the second argument.

Conclusion

The PHP developers use the print_r() function mainly to check the content of the variable for debugging purposes. The uses of the print_r() function to print the primitive and the compound data have been described in this tutorial for helping the PHP users to use this function properly in their script.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.