Linux and UNIX Man Pages

Linux & Unix Commands - Search Man Pages

var_export(3) [php man page]

VAR_EXPORT(3)								 1							     VAR_EXPORT(3)

var_export - Outputs or returns a parsable string representation of a variable

SYNOPSIS
mixed var_export (mixed $expression, [bool $return = false]) DESCRIPTION
var_export(3) gets structured information about the given variable. It is similar to var_dump(3) with one exception: the returned represen- tation is valid PHP code. PARAMETERS
o $expression - The variable you want to export. o $return - If used and set to TRUE, var_export(3) will return the variable representation instead of outputting it. RETURN VALUES
Returns the variable representation when the $return parameter is used and evaluates to TRUE. Otherwise, this function will return NULL. NOTES
Note When the $return parameter is used, this function uses internal output buffering so it cannot be used inside an ob_start(3) callback function. CHANGELOG
+--------+---------------------------------------------------+ |Version | | | | | | | Description | | | | +--------+---------------------------------------------------+ | 5.1.0 | | | | | | | Possibility to export classes and arrays con- | | | taining classes using the __set_state() magic | | | method. | | | | +--------+---------------------------------------------------+ EXAMPLES
Example #1 var_export(3) Examples <?php $a = array (1, 2, array ("a", "b", "c")); var_export($a); ?> The above example will output: array ( 0 => 1, 1 => 2, 2 => array ( 0 => 'a', 1 => 'b', 2 => 'c', ), ) <?php $b = 3.1; $v = var_export($b, true); echo $v; ?> The above example will output: 3.1 Example #2 Exporting classes since PHP 5.1.0 <?php class A { public $var; } $a = new A; $a->var = 5; var_export($a); ?> The above example will output: A::__set_state(array( 'var' => 5, )) Example #3 Using __set_state() (since PHP 5.1.0) <?php class A { public $var1; public $var2; public static function __set_state($an_array) { $obj = new A; $obj->var1 = $an_array['var1']; $obj->var2 = $an_array['var2']; return $obj; } } $a = new A; $a->var1 = 5; $a->var2 = 'foo'; eval('$b = ' . var_export($a, true) . ';'); // $b = A::__set_state(array( // 'var1' => 5, // 'var2' => 'foo', // )); var_dump($b); ?> The above example will output: object(A)#2 (2) { ["var1"]=> int(5) ["var2"]=> string(3) "foo" } NOTES
Note Variables of type resource couldn't be exported by this function. Note var_export(3) does not handle circular references as it would be close to impossible to generate parsable PHP code for that. If you want to do something with the full representation of an array or object, use serialize(3). Warning When var_export(3) exports objects, the leading backslash is not included in the class name of namespaced classes for maximum com- patibility. SEE ALSO
print_r(3), serialize(3), var_dump(3). PHP Documentation Group VAR_EXPORT(3)

Check Out this Related Man Page

SERIALIZE(3)								 1							      SERIALIZE(3)

serialize - Generates a storable representation of a value

SYNOPSIS
string serialize (mixed $value) DESCRIPTION
Generates a storable representation of a value. This is useful for storing or passing PHP values around without losing their type and structure. To make the serialized string into a PHP value again, use unserialize(3). PARAMETERS
o $value - The value to be serialized. serialize(3) handles all types, except the resource-type. You can even serialize(3) arrays that con- tain references to itself. Circular references inside the array/object you are serializing will also be stored. Any other refer- ence will be lost. When serializing objects, PHP will attempt to call the member function __sleep() prior to serialization. This is to allow the object to do any last minute clean-up, etc. prior to being serialized. Likewise, when the object is restored using unserialize(3) the __wakeup() member function is called. Note Object's private members have the class name prepended to the member name; protected members have a '*' prepended to the member name. These prepended values have null bytes on either side. RETURN VALUES
Returns a string containing a byte-stream representation of $value that can be stored anywhere. Note that this is a binary string which may include null bytes, and needs to be stored and handled as such. For example, serialize(3) out- put should generally be stored in a BLOB field in a database, rather than a CHAR or TEXT field. EXAMPLES
Example #1 serialize(3) example <?php // $session_data contains a multi-dimensional array with session // information for the current user. We use serialize() to store // it in a database at the end of the request. $conn = odbc_connect("webdb", "php", "chicken"); $stmt = odbc_prepare($conn, "UPDATE sessions SET data = ? WHERE id = ?"); $sqldata = array (serialize($session_data), $_SERVER['PHP_AUTH_USER']); if (!odbc_execute($stmt, $sqldata)) { $stmt = odbc_prepare($conn, "INSERT INTO sessions (id, data) VALUES(?, ?)"); if (!odbc_execute($stmt, $sqldata)) { /* Something went wrong.. */ } } ?> NOTES
Note Note that many built-in PHP objects cannot be serialized. However, those with this ability either implement the Serializable inter- face or the magic __sleep() and __wakeup() methods. If an internal class does not fulfill any of those requirements, it cannot reli- ably be serialized. There are some historical exceptions to the above rule, where some internal objects could be serialized without implementing the interface or exposing the methods. Notably, the ArrayObject prior to PHP 5.2.0. Warning When serialize(3) serializes objects, the leading backslash is not included in the class name of namespaced classes for maximum compatibility. SEE ALSO
unserialize(3), var_export(3), json_encode(3), Serializing Objects, __sleep(), __wakeup(). PHP Documentation Group SERIALIZE(3)
Man Page