• PHP Video Tutorials

PHP - var_export() Function



Definition and Usage

The function var_export() outputs or returns a parsable string representation of a variable. It is similar to var_dump() with one exception, the returned representation is valid PHP code.

Syntax

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

Parameters

Sr.No Parameter Description
1

value

Mandatory.The variable you want to export.

2

return

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

Return Values

This function returns the variable representation when the return parameter is set to true. Otherwise, this function will return null.

Dependencies

PHP 4.2 and above

Example

Following example demonstrates use of var_export():

  <?php
  $a = "Welcome TutorialsPoint!";
  var_export($a);
  echo "<br>";
  $b = array(2,'hello',22.99, array('a','b',2));
  var_export($b);
  echo "<br>";

  // Create a class
  class tutorialsPoint {
      public $tp_name =
            "Welcome to TutorialsPoint";
  }

  // Create the class name alias
  class_alias('tutorialsPoint', 'TP');

  $obj1 = new tutorialsPoint();
  var_export($obj1);
  ?>

Output

This will produce following result −

  'Welcome TutorialsPoint!'
  array ( 0 => 2, 1 => 'hello', 2 => 22.99, 3 => array ( 0 => 'a', 1 => 'b', 2 => 2, ), )
  tutorialsPoint::__set_state(array( 'tp_name' => 'Welcome to TutorialsPoint', ))
Advertisements