PHP print_r Function | Print Variable in Human Readable Form

PHP print_r is an inbuilt function in PHP. It prints the contents of a variable in human-readable form. Mostly, it is used to print a PHP Array in a readable form. In this article, we will discuss the PHP print_r Function. Also, we will discuss a few examples of using it.

Syntax

print_r( $variable, $return = false);

Parameters

The PHP print_r Function expects two parameters. However, one parameter is mandatory while the other is optional to pass. The description of the parameters is as follows:

  • $variable: The first parameter to the function is mandatory and specifies the variable which the function will print.
  • $return: The second parameter is optional and specifies whether to return the value of print it. It can take two possible values:
    • False: The function will print the contents of the variable. Also, this is the default value of this parameter.
    • True: The print_r function will return the contents of the variable to another variable.

Return Value

If the variable is a string or array, then print_r prints the value. However, if the variable is an array, then it prints the contents of the array with keys and values in human-readable form. Similarly, with objects, it prints the all the member variables of that object.

However, if the return value is set to true, then print_r does not print but returns the values to another variable. This functionality is rarely used in practice.

PHP print_r Function Diagram

Examples

Let’s discuss a few examples of using the PHP print_r Function.

Example 1: Simple String or Integer

<?php

	$string = 'Concatly';

	$integer = 2;

	print_r($string);
	/* Concatly */

	print_r($integer);
	/* 2 */
?>

You can use the print_r to print the values of a string or an integer.

Example 2: Print Array

Similarly, you can also print a PHP Array using the function.

<?php

	$testArray = array(
		'a' => 'apple',
		'b' => 'ball',
		'c' => array(
			'ca' => 'cat',
			'co' => 'cold'
		)
	);

	print_r($testArray);
	/*
	Array
	(
	    [a] => apple
	    [b] => ball
	    [c] => Array
	        (
	            [ca] => cat
	            [co] => cold
	        )

	)
	*/

?>

As you can see above, the function prints all the keys and values of the array in human-readable form.

Example 3: Print Object

Likewise, you can also print an object using PHP print_r Function and see all the member variables of the class.

<?php

	class TestClass {

		private $testVariable1;

		public $testVariable2 = 10;

	}

	$testObject = new TestClass();

	print_r($testObject);
	/*
	TestClass Object
	(
	    [testVariable1:TestClass:private] => 
	    [testVariable2] => 10
	)
	*/

?>

Example 4: Returning Value

You can also pass the return parameter as true to make the function return the contents of the variable instead of printing them. However, this functionality is rarely used.

<?php

	$testArray = array(
		'a' => 'apple',
		'b' => 'ball',
		'c' => array(
			'ca' => 'cat',
			'co' => 'cold'
		)
	);

	$variable = print_r($testArray, true);
	echo $variable;
        // "Array ( [a] => apple [b] => ball [c] => Array ( [ca] => cat [co] => cold ) )"
        // This a string, not an array
?>

Note: The function uses internal output buffering to return the value of the variable. In the above example, the return value is actually a string not an array.

Conclusion

In conclusion, we discussed the PHP print_r Function. It is one of the commonly used functions in PHP and prints the contents in a variable. You can read more about it on PHP Official Documentation. Also, you can read more articles on PHP on Concatly.

Spread the Knowledge
Categories:

Leave a Reply

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