Php

PHP print_r() Function With Example

This PHP tutorial helps you to learn basic print_r statement with examples. The print_r() PHP function is used to return an array in a human readable form.

The print_r() displays information about a variable in a way that’s readable by humans.

pre tag

We’ll use pre tag to display print_r() method returned value. The ‘pre’ tag denotes that the code has been preformatted. The text will be shown in a fixed-width font with this tag. It maintains line breaks and spaces, making it easier to read for humans.

Syntax :
print_r(var_name, return_output)

Related Post
  • var_name : The variable being printed.
  • return_output : It helps to capture the output in a variable, the parameter should set TRUE. The default value is FALSE.

Return Value:

If the variable is an integer or a float or a string the function returns the value of the variable.
If the variable is an array the function returns keys and elements, a similar notation is used for the object. Setting TRUE to return_output parameter the function returns a string.

Sample Example PHP print_r()

<?php
$var1 = 'phpflow';
$var2 = 53.55;
$var3 = 53;
$var4 = array('emp_name'=>'Reven','Age'=>34,'Salary'=>23243,'Depts'=>array(5,6,7,8));

print_r($var1);echo"<br/>";
print_r($var2);echo"<br/>";
print_r($var3);echo"<pre>";
print_r($var4);
?>

Output :

phpflow
53.55
53
Array
(
    [emp_name] => Reven
    [Age] => 34
    [Salary] => 23243
    [Depts] => Array
        (
            [0] => IT
            [1] => Admin
        )

)

The print_r(), var_dump() and var_export() will also show protected and private properties of objects. Static class members will not be shown.
The difference of the var_dump() and var_export() is that var_export returns valid PHP code, whereas var_dump does not.

Recent Posts

Configure and Retrieve S3 Information Using Laravel PHP-AWS-SDK

This tutorial helps integrate a PHP SDK with Laravel. We'll install aws-php-sdk into laravel application and access all aws services… Read More

3 months ago

What is the Purpose of php_eol in PHP?

in this quick PHP tutorial, We'll discuss php_eol with examples. PHP_EOL is a predefined constant in PHP and represents an… Read More

9 months ago

Laravel Table Relationship Methods With Example

This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More

9 months ago

Exploring the Power of Laravel Eloquent Join?

We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More

9 months ago

Quick and Easy Installation of Laravel Valet

in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More

10 months ago

What is Laravel Soft Delete and How Does it Work?

I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More

10 months ago

Categories