PHP - var_export() 函式



定義和用法

var_export() 函式輸出或返回變數的可解析字串表示形式。它類似於 var_dump(),但有一個例外,返回的表示形式是有效的 PHP 程式碼。

語法

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

引數

序號 引數 描述
1

必需。要匯出的變數。

2

返回

可選。如果使用並設定為 true,則 var_export() 將返回變量表示形式,而不是輸出它。

返回值

return 引數設定為 true 時,此函式返回變量表示形式。否則,此函式將返回 null

依賴項

PHP 4.2 及更高版本

示例

以下示例演示了 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);
  ?>

輸出

這將產生以下結果:

  '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', ))
廣告