PHP returns a string representation of a variable or array: Var_export ()

Source: Internet
Author: User

Use the var_export () function to view the contents of variables and arrays in the case where the server program is not printing, with the file_put_contents convenient debugger.

In the development process, we often use Var_dump () to print the array contents, but sometimes we do not have the convenience of viewing debugging information through the browser, when the information can be exported to a file to view, Var_export () can print or return a variable string representation, the return value is a string, In the form of a Var_dump () printed string, use Var_export () to write data such as post, get, and session to the file for easy viewing.

Here is the description in the PHP documentation:

Mixed Var_export ( mixed $expression [, BOOL $return])

This function returns the structure information about the variable passed to the function, similar to Var_dump (), unlike the PHP code whose returned representation is valid.

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

  

In short, when the second argument is TRUE , There is a return value that does not print, or false when the variable is printed , and the default is false

The following example uses Var_export () to convert a variable to a string after the output:

<?PHP$num= 255;$str= ' abc Def ';$bool=true;$arr=Array(' value1 ', ' value2 ', 1, ' key1 ' = ' value3 ', ' key2 ' =Array(2, 3));Echo Var_export($num, TRUE);Echo"\ n";Echo Var_export($str, TRUE);Echo"\ n";Echo Var_export($bool, TRUE);Echo"\ n";Echo Var_export($arr, TRUE);Echo"\ n";/*Output 255 ' abc def ' truearray (0 = ' value1 ', 1 = ' value2 ', 2 = 1, ' key1 ' = ' value3 ', ' key2 ' = arr Ay (0 = 2, 1 = 3,),)

Var_export () returns a valid PHP code, very convenient to generate configuration files or cache files, the following with a simple cache file example:

<?PHP//The $cache value is taken from the database when the cache is updated$cache=Array(    ' Log_record ' =false, ' log_type ' = ' File ', ' log_level ' = ' emerg,alert,crit,err ', ' log_file_size ' = = 2097152, ' log_exception_record ' =false,); //writes data read from the database to the cache file$content= "<?php\nreturn".Var_export($cache);//use Var_export () herefile_put_contents('./cache.php ',$content);

Our cache file cache.php content is as follows:

<? PHP return Array (    ' log_record '            =  false,    ' log_type '              = '  File ',     ' Log_ Level '             = '  emerg,alert,crit,err ',    ' log_file_size '         = '  2097152,    ' Log_ Exception_record '  =  false,

When you call the cache file, you only need to use require () to copy the contents of the cache file to a variable:

<? PHP $cache require ('./cache.php ');

The array in the cache file is assigned to the $cache variable, and the Phpcms cache file is cached in this way.

  

  

  

PHP returns a string representation of a variable or array: Var_export ()

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.