PHP Variable Handling var_export() Function



The PHP Variable Handling var_export() function is used to show a variable's structure. It works similarly to var_dump(), except the output is true PHP code. This means that you can copy and paste the output into your PHP scripts. The results can be printed directly or returned as a string. If you set the second parameter to true, it will output the result.

If you do not specify true, it will merely print the output. It is useful for debugging and storing variable values in a file. It supports arrays, objects, and a number of additional variables.

Syntax

Below is the syntax of the PHP Variable Handling var_export() function −

string|null var_export ( mixed $value , bool $return = false )

Parameters

Below are the parameters of the var_export() function −

  • $value − (Required) It is the variable you want to export.

  • $return − (Optional) If used and set to true, var_export() will return the variable representation instead of outputting it.

Return Value

The var_export() function returns the variable representation when the return parameter is set to TRUE. Otherwise, this function will return NULL.

PHP Version

First introduced in core PHP 4.2.0, the var_export() function continues to function easily in PHP 5, PHP 7, and PHP 8.

Example 1

First we will show you the basic example of the PHP Variable Handling var_export() function with a basic string variable. It will directly output the variable's structured representation in valid PHP code.

<?php
  // Define a string variable
  $name = "Sonali Sharma";

  // Use var_export() to display the structure
  var_export($name);
?>

Output

Here is the outcome of the following code −

'Sonali Sharma'

Example 2

This program uses an array in the var_export() function. It explains how to represent an array in proper PHP format. You can copy and paste the results into another script.

<?php
  // Define an array
  $numbers = [1, 2, 3, 4, 5];

  // Use var_export() to display the array structure
  var_export($numbers);
?> 

Output

This will generate the below output −

array (
  0 => 1,
  1 => 2,
  2 => 3,
  3 => 4,
  4 => 5,
)

Example 3

Now in the below code Here, var_export() function returns the output instead of printing it. We will store the result in a variable and then use file_put_contents() to save it in a file. This is useful when you want to save variable values for later use.

<?php
  // Define an associative array
  $data = ["name" => "Alia", "age" => 25, "city" => "New Delhi"];

  // Store var_export() output in a variable
  $output = var_export($data, true);

  // Save the output in a file
  file_put_contents("/PHP/PhpProjects/newfile.php", "<?php\nreturn $output;\n?>");
  echo "Content has been saved in the given file.";
?> 

Output

This will create the below output −

Content has been saved in the given file.

Here is the saved content in the newfile.php −

<?php
return array (
  'name' => 'Alia',
  'age' => 25,
  'city' => 'New Delhi',
);
?>

Example 4

In the following example, we are using the var_export() function to work with objects. It exports the object structure so it can be reused later. This is helpful for debugging or saving object data.

<?php
  // Define a simple class
  class Person {
    public $name;
    public $age;
    
    public function __construct($name, $age) {
        $this->name = $name;
        $this->age = $age;
    }
  }

  // Create an object
  $person = new Person("Bob", 30);

  // Export the object
  var_export($person);
?> 

Output

Following is the output of the above code −

\Person::__set_state(array(
   'name' => 'Bob',
   'age' => 30,
))
php_variable_handling_functions.htm
Advertisements