Monday, November 18, 2024
Google search engine
HomeLanguagesWhat is the difference between var_dump() and print_r() in PHP ?

What is the difference between var_dump() and print_r() in PHP ?

In this article, we will discuss the difference between var_dump() and print_r() function in PHP.

var_dump() Function: The var_dump() function is used to dump information about a variable that displays structured information such as the type and value of the given variable.

Syntax:

void var_dump ($expression)

Parameters:

  • $expression: It can be one single variable or an expression containing several space-separated variables of any type.

Return Value: This function has no return type.

 

Example: PHP code that demonstrates the working of var_dump() function.

PHP




<?php
  
// Using var_dump function on
// different data type variables
var_dump(var_dump(45, 62.1, TRUE, 
    "sravan", array(1, 2, 3, 4,5,6))
);
  
?>


Output:

int(45) float(62.1) bool(true) string(6) "sravan" array(6) 
{ [0]=> int(1) [1]=> int(2) [2]=> int(3) 
  [3]=> int(4) [4]=> int(5) [5]=> int(6) } NULL

print_r() Function: The print_r() function is a built-in function in PHP and is used to print information stored in a variable.

Syntax:

print_r( $variable, $isStore )

Parameters: This function accepts two parameters as shown in the above syntax and described below.

  • $variable: This parameter specifies the variable to be printed and is a mandatory parameter.
  • $isStore: This is an optional parameter. This parameter is of the boolean type whose default value is FALSE and is used to store the output of the print_r() function in a variable rather than printing it. If this parameter is set to TRUE then the print_r() function will return the output which it is supposed to print.

Return Value: If the $variable is an integer or a float or a string, the function prints the value of the variable. If the variable is an array the function prints the array in a format that displays the keys as well as values, a similar notation is used for objects. If the parameter $isStore is set to TRUE, then the print_r() function will return a string.

Example: PHP code to display all data type variables using print_r() function.

PHP




<?php
  
  // String variable
  $a = "Welcome to neveropen";
  
  // Integer variable
  $b = 450;
  
  // Array variable
  $arr = array('0' => "Computer"
               '1' => "science"
               '2' => "portal");
  
  // Printing the variables
  print_r($a);
  echo"\n<br>";
  print_r($b);
  echo"\n<br>";
  print_r($arr);
?>


Output:

Welcome to neveropen 
450
Array ( [0] => Computer [1] => science [2] => portal )

Difference between var_dump() and print_r() functions:

                                      var_dump()                                       print_r()
var_dump() displays values along with data types as output. print_r() displays only value as output.
It does not have any return type. It will return a value that is in string format.
The data returned by this function is difficult to understand. The data returned by this function is human-readable.
This function can be used for debugging purposes. This function is used with database and web applications.
var_dump() will display the number of elements in a variable. print_r() will not display the number of elements in a variable.
var_dump() will display the length of the variable. print_r() will not display the length of the variable.

RELATED ARTICLES

Most Popular

Recent Comments