How to use the Var_export function

Source: Internet
Author: User

The Var_export () function returns the structure information about the variable passed to the function, similar to Var_dump (), unlike the PHP code whose returned representation is legitimate. Var_export must return a valid PHP code, that is, the code returned by Var_export can be directly assigned to a variable as PHP code. And this variable gets the same value as the Var_export type. Let's look at a simple example:

<?php$arr = Array (1, 2, Array ("Apple", "banana", "orange"));  Var_export ($arr);  ? >

Program output:

Array (  0 = 1,  1 = 2,  2 = =   Array (    0 = ' Apple ',    1 = ' banana ',    2 = ' Oran GE ',  ),)

Note that the above output is a valid PHP code. If Var_dump () is used, the output is:

Array (3) {  [0]=>  int (1)  [1]=>  int (2)  [2]=>  Array (3) {    [0]=>    string (5 ) "Apple"    [1]=>    string (6) "Banana"    [2]=>    string (6) "Orange"  }}

You can return the representation of a variable by setting the second argument of the function to TRUE.

<?php$v = ' nowamagic ';  $rs = Var_export ($v, TRUE);  Echo $rs;? >

Program Run Result:

' Nowamagic '

Note two points:

    • Var_export () retains the structured form of storing data.
    • But in particular, remember that the type of the variable value at this point is already a string ($var), and it is no longer possible to take the value out of the array.

In the Phpcms source code, you can see that many of the configuration parameters are recorded in an array, including their channels, content, and so on.

function Cache_write ($file, $string, $type = ' array ')  {      if (Is_array ($string))      {          $type = Strtolower ($ type);          if ($type = = ' array ')          {              $string = "<?php\n return". Var_export ($string, TRUE). "; N?> ";          }          ElseIf ($type = = ' constant ')          {              $data = ';              foreach ($string as $key = = $value) $data. = "define ('". Strtoupper ($key). "', '".  Addslashes ($value). "'); \ n ";              $string = "<?php\n". $data. " \n?> ";          }      }      $strlen = File_put_contents (phpcms_cachedir. $file, $string);      chmod (Phpcms_cachedir. $file, 0777);      return $strlen;  }  
Extended Reading

The list of topics for this article is as follows:

  1. PHP function Completion: GET_MAGIC_QUOTES_GPC ()
  2. PHP function Completion: error_reporting ()
  3. PHP function Completion: Preg_match ()
  4. PHP function Completion: UrlEncode ()
  5. PHP function Completion: Array_multisort ()
  6. PHP function Completion: Array_splice ()
  7. PHP function Completion: isset ()
  8. PHP function Completion: getenv ()
  9. PHP function Completion: header ()
  10. PHP function Completion: mysql_num_rows ()
  11. PHP function Completion: List ()
  12. PHP function Completion: mysql_query ()
  13. PHP function Completion: mysql_fetch_array ()
  14. PHP function Completion: Number_format ()
  15. PHP function Completion: Explode ()
  16. PHP function Completion: Call_user_func ()
  17. PHP function Completion: Imagecopyresamples ()
  18. PHP function Completion: Import_request_variables ()
  19. PHP function Completion: Parse_url ()
  20. PHP function Completion: Remove HTML tag strip_tags ()
  21. PHP function Completion: output array structure and content var_dump ()
  22. PHP function Completion: Var_export ()
  23. PHP function Completion: Determine whether the variable is a digital is_numeric ()
  24. PHP function Completion: Session_name ()
  25. PHP function Completion: session_id ()
  26. PHP function Completion: NL2BR () and nl2p () function
  27. PHP function Completion: Shuffle () take an array of random elements
  28. PHP function Completion: http_build_query () constructs URL string
  29. PHP function Completion: stream_context_create () Analog post/get

How to use the Var_export function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.