- PHP Basics
- Learn PHP
- PHP Comments
- PHP Data Types
- PHP Variables
- PHP Operators
- PHP echo
- PHP print
- PHP echo vs. print
- PHP if else
- PHP switch
- PHP for Loop
- PHP while Loop
- PHP do...while Loop
- PHP foreach Loop
- PHP break and continue
- PHP exit()
- PHP exit() vs. break
- PHP isset()
- PHP Arrays
- PHP print_r()
- PHP unset()
- PHP Strings
- PHP Functions
- PHP File Handling
- PHP File Handling
- PHP Open File
- PHP Create a File
- PHP Write to File
- PHP Read File
- PHP feof()
- PHP fgetc()
- PHP fgets()
- PHP Close File
- PHP Delete File
- PHP Append to File
- PHP Copy File
- PHP file_get_contents()
- PHP file_put_contents()
- PHP file_exists()
- PHP filesize()
- PHP Rename File
- PHP fseek()
- PHP ftell()
- PHP rewind()
- PHP disk_free_space()
- PHP disk_total_space()
- PHP Create Directory
- PHP Remove Directory
- PHP Get Files/Directories
- PHP Get filename
- PHP Get Path
- PHP filemtime()
- PHP file()
- PHP include()
- PHP require()
- PHP include() vs. require()
- PHP mysqli Tutorial
- PHP mysqli Tutorial
- PHP and MySQL Setup
- PHP mysqli: Create Database
- PHP mysqli: Create Table
- PHP mysqli: Insert Record
- PHP mysqli: Update Record
- PHP mysqli: Fetch Record
- PHP mysqli: Delete Record
- PHP mysqli: SignUp Page
- PHP mysqli: LogIn Page
- PHP mysqli: Store User Data
- PHP mysqli Functions
- PHP mysqli_connect()
- PHP mysqli_close()
- PHP mysqli_connect_errno()
- PHP mysqli_connect_error()
- PHP mysqli_query()
- PHP mysqli_fetch_row()
- PHP mysqli_fetch_assoc()
- PHP mysqli_fetch_array()
- PHP mysqli_free_result()
- PHP mysqli_error()
- PHP mysqli_prepare()
- PHP mysqli_stmt_bind_param()
- PHP mysqli_stmt_execute()
- PHP mysqli_stmt_fetch()
- PHP mysqli_stmt_store_result()
- PHP mysqli_stmt_num_rows()
- PHP mysqli_stmt_bind_result()
- PHP mysqli_stmt_get_result()
- PHP mysqli_result class
- PHP mysqli_report()
- PHP error_reporting()
- PHP mysqli_real_escape_string()
- PHP htmlspecialchars()
- PHP Misc Topics
- PHP Object Oriented
- PHP new Keyword
- PHP header()
- PHP getallheaders()
- PHP Cookies
- PHP Sessions
- PHP Date and Time
- PHP GET vs. POST
- PHP File Upload
- PHP Image Processing
PHP print_r() function
The PHP "print_r()" function is used to print information of a specified variable and looks more readable. Or we can say that the print_r() function is used to print an array, object, or variable's contents in a human-readable format. Debugging and comprehending the composition and contents of complex data types are facilitated by it. For example:
<?php $x = array("Berlin", "London", "NewYork", "Munich"); print_r($x); ?>
The output of the above PHP example on the print_r() function is:
See the output of $x, an array variable. We can clearly see that, at index 0, the Berlin is stored. Similarly, at index 1, London is stored, and so on. That is, if you use the following PHP statement:
echo $x[0];
Then you will get Berlin as an output.
PHP print_r() syntax
The syntax of the print_r() function in PHP is:
print_r(variable, return)
The first parameter (variable) is required, whereas the second parameter (return) is optional.
The "variable" parameter is used to specify the variable about which we want to print information.
Note: The default value of the return parameter is false. But if we specify this parameter as true, then the function print_r() returns the information without printing.
Note: If the variable parameter is an integer, float, or string type value, then the function print_r() prints the value itself. Whereas if the variable parameter is an array or an object, then it returns the information about the variable in the form of keys and elements.
Advantages of the print_r() function in PHP
- Simple and straightforward to use: print_r() can be used to quickly print the contents of an array or object.
- Human-readable output: The output of print_r() is formatted to be simple to read and comprehend for people. This makes it an excellent tool for troubleshooting and comprehending intricate data structures.
- print_r() is capable of handling nested arrays and objects, which can be challenging to print out using other techniques.
Disadvantages of the print_r() function in PHP
- print_r() only prints the contents of an array or object, which limits its functionality. It does not offer any extra features, like sorting or filtering.
- Not recommended for large data sets: print_r()'s output can quickly become overwhelming for very large data sets, making it difficult to read and understand.
- Security issues: If print_r() is used incorrectly, it could give hackers access to confidential data. As a result, it shouldn't be used in production code and should only be used for debugging.
« Previous Tutorial Next Tutorial »