print_r()
The print_r() function is a built-in function in PHP that prints a human-readable representation of a variable to the output. It is commonly used for debugging
Introduction
The print_r() function is a built-in function in PHP that prints a human-readable representation of a variable to the output. It is commonly used for debugging purposes to inspect the contents of a variable.
Syntax
The syntax of the print_r() function is as follows:
The PHP syntax of the print_r()
void print_r(mixed $var [, bool $return = false ])The function takes one or two parameters. The first parameter, $var, represents the variable to be printed. The second parameter, $return, is an optional boolean parameter that determines whether the function should return the output as a string instead of printing it to the output. If $return is true, the function returns the output as a string. Otherwise, it prints the output to the output buffer.
Example Usage
Here is an example of how to use the print_r() function in PHP:
Example of PHP print_r()
<?php
$array = ["apple", "banana", "cherry"];
print_r($array);
?>In this example, we define an array $array containing three elements. We use the print_r() function to print the contents of the array to the output. The output shows the contents of the array in a human-readable format:
Array
(
[0] => apple
[1] => banana
[2] => cherry
)Conclusion
The print_r() function is a useful tool for printing the contents of a variable to the output in a human-readable format. It can be used for debugging purposes or for displaying data to users. By using this function, developers can inspect the contents of a variable and ensure that their code is working with the expected data.
Practice
What does the print_r() function in PHP do?