PHP began as a small open source project that grew in popularity as more people realized how beneficial it was. In 1994, Rasmus Lerdorf released the initial version of PHP. PHP is the recursive abbreviation for "PHP: Hypertext Preprocessor."

PHP is an HTML-enabled server-side programming language. It's used to manage dynamic content, databases, and session monitoring, as well as for web development in general. MySQL, PostgreSQL, Oracle, Sybase, Informix, and Microsoft SQL Server are just a few of the databases it supports. PHP runs quickly, especially when compiled as an Apache module on the Unix side.

What is the Use of print_r() Function in PHP?

To inspect or check the structure and values of an array in a human-readable format on the screen, you can use the print r() or var dump() statements in PHP. var dump() usually provides more information than print_r in PHP.

print_r(variable, isStore)

It is a built-in function in print_r in PHP that is used to print or display the contents of a variable. It essentially prints human-readable data about a variable. The value of the variable will be printed if it is a string, integer, or float. If you provide an array, it will show the values in a format that includes keys and elements. Objects have a similar notation.

  • variable: This is a required argument that specifies which variable you should print.
  • isStore: It is a boolean parameter with the default value of FALSE that is used to save the result of the print_r in PHP function in a variable instead of printing it.

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

Example

<?php

$str = "This is a string"; //string

$integ = 30;//integer

$arra = array('0' => "Akash", '1' => "Sarla", '2' => "Golu");// array

print_r($str);

echo "<br>";

print_r($integ);

echo"<br>";

print_r($arra);

?>

Output

Print_r_Function_In_PHP_1

Syntax of print_r() Function

print_r(variable, isStore)

<?php

// PHP program to illustrate

// the print_r() function

// string variable

$var1 = "Welcome to GeeksforGeeks";

// integer variable

$var2 = 101;

// array variable

$arr = array('0' => "How", '1' => "Are", '2' => "You");

// printing the variables

print_r($var1);

echo"\n";

print_r($var2);

echo"\n";

print_r($arr);

?>

Output

Print_r_Function_In_PHP_2

Explain the Parameter Values Along With Their Use

There are two parameters to the print_r in PHP method, which are detailed below:

  • var name is a mandatory parameter. This parameter specifies the variable for which the information is to be returned.
  • return output - This is a parameter that can be omitted. If you want to save the output of the print_r in the PHP function, use the return value parameter. It's a boolean argument with the value FALSE by default.
  • If the provided variable is an integer, float, or string, it returns and prints the variable's value.
  • If the variable in question is an array, it will print the values as keys and values. Objects can be represented in the same way.
  • This function will return a string if it sets the $return output option to TRUE. Otherwise, TRUE will be returned.

How to Implement print_r in PHP?

The code snippet below shows how to use print_r in PHP to display information about a string variable, an integer variable, and an array input. You can accomplish this by calling the function with simply the input variable as an input parameter. The returnType parameter's default value is FALSE in this circumstance.

Example

<?php

// PHP program to illustrate the print_r() function to exhibit printing functionality :

// Declaring a string variable

$input_str = "An information string";

// Declaring a  integer variable

$input_int= 101;

// Declaring an array variable

$input_arr = array("Count1"=>"78", "Count2"=>"98", "Count3"=>"41");

// printing the variables

print_r("Printing the string variable information");

echo"\n";

print_r($input_str);

echo"\n";

echo"\n";

print_r("Printing the integer variable information");

echo"\n";

print_r($input_int);

echo"\n";

echo"\n";

print_r("Printing the array variable information");

echo"\n";

print_r($input_arr);

?>

Output

Print_r_Function_In_PHP_3.

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

Example

<?php

// PHP program to illustrate the print_r in PHP function exhibiting the storing functionality

// Declaring a string variable

$input_str = "An information string";

// Declaring an integer variable

$input_int= 5040;

// Declaring an array variable

$input_arr = array("element1"=>"52", "element2"=>"14", "element3"=>"85");

// Capturing and storing the output in different variables

print_r("Capturing the integer variable information");

echo"\n";

//Storing the integer variable output

$input_int_cap=print_r($input_int,true);

print_r($input_int_cap);

echo"\n";

echo"\n";

print_r("Capturing the string variable information");

echo"\n";

//Storing the string variable output

$input_str_cap=print_r($input_str,true);

print_r($input_str_cap);

echo"\n";

echo"\n";

print_r("Capturing the array variable information");

echo"\n";

//Storing the array variable output

$input_arr_cap=print_r($input_arr,true);

print_r($input_arr_cap);

?>

Output

Print_r_Function_In_PHP_4

The information from integer and string variables is saved in integer and string data type storing variables, respectively. Whereas, it stores the output from the array variable in array type storing variables as key and element, together with the data type associated with it.

  • This function can also display properties of an object's private and protected properties, however, it has limitations because it does not display results for static class members.
  • The Reflection class in PHP5 allows you to utilize print r() for static class member variables.
  • print_r in PHP is a debugging function for PHP scripts.

Conclusion 

This was a complete guide to understand the print_r in PHP command. You looked into the concepts with the help of examples to help you reach out to the core of the language. If you are planning to pursue a career in the PHP field, then it becomes a necessity to understand the process in detail. You can enroll yourself with Simplilearn's Full Stack Web Development Course. We will also guide you to enroll in skill-up courses that are available so that you can have a better understanding and bring out the best from your knowledge. This will also help you get hired in the best jobs. 

Have any questions for us? Leave them in the comments section of this article. Our experts will get back to you on the same, as soon as possible!

Our Software Development Courses Duration And Fees

Software Development Course typically range from a few weeks to several months, with fees varying based on program and institution.

Program NameDurationFees
Caltech Coding Bootcamp

Cohort Starts: 16 Dec, 2024

6 Months$ 8,000
Automation Test Engineer Masters Program

Cohort Starts: 27 Nov, 2024

8 months$ 1,499
Full Stack Java Developer Masters Program

Cohort Starts: 18 Dec, 2024

7 months$ 1,449
Full Stack (MERN Stack) Developer Masters Program

Cohort Starts: 8 Jan, 2025

6 Months$ 1,449