Example 6-3. The Log.inc file
Store the Log class definition in a file called Log.inc. The HTML page in Example 6-4 uses the Log class and PHP sessions to create a persistent log variable, $l.
Description
The serialize[] converts a storable representation of a value.
A serialize data means a sequence of bits so that it can be stored in a file, a memory buffer, or transmitted across a network connection link.
Version:
[PHP 4 and above]
Syntax:
serialize[value1]
Parameter:
value1 | The value to be serialized | Required | Mixed* |
*Mixed: Mixed indicates that a parameter may accept multiple [but not necessarily all] types.
Return value:
A string.
Value Type: String.
Example:
Output:
a:3:{i:0;s:4:"Math";i:1;s:8:"Language";i:2;s:7:"Science";}
View the example in the browser
Practice here online :
See also
PHP Function Reference
Previous: print_r
Next: settype
PHP: Tips of the Day
PHP: How to generate a random, unique, alphanumeric string for use in a secret link?
Security Notice: This solution should not be used in situations where the quality of your randomness can affect the security of an application. In particular, rand[] and uniqid[] are not cryptographically secure random number generators. See Scott's answer for a secure alternative.
If you do not need it to be absolutely unique over time:
md5[uniqid[rand[], true]]
Otherwise [given you have already determined a unique login for your user]:
md5[uniqid[$your_user_login, true]]
Ref : //bit.ly/31fd9wa
A PHP array or object or other complex data structure cannot be transported or stored or otherwise used outside of a running PHP script. If you want to persist such a complex data structure beyond a single run of a script, you need to serialize it. That just means to put the structure into a "lower common denominator" that can be handled by things other than PHP, like databases, text files, sockets. The standard PHP function
serialize
is just a format to express such a thing, it serializes a data structure into a string representation that's unique to PHP and can be reversed into a PHP object using unserialize
. There are many other formats though, like JSON or XML.
Take for example this common problem:
How do I pass a PHP array to Javascript?
PHP and Javascript can only communicate via strings. You can pass the string "foo"
very easily to
Javascript. You can pass the number 1
very easily to Javascript. You can pass the boolean values true
and false
easily to Javascript. But how do you pass this array to Javascript?
Array [ [1] => elem 1 [2] => elem 2 [3] => elem 3 ]
The answer is serialization. In case of PHP/Javascript, JSON is actually the better serialization format:
{ 1 : 'elem 1', 2 : 'elem 2', 3 : 'elem 3' }
Javascript can easily reverse this into an actual Javascript array.
This is just as valid a representation of the same data structure though:
a:3:{i:1;s:6:"elem 1";i:2;s:6:"elem 2";i:3;s:7:" elem 3";}
But
pretty much only PHP uses it, there's little support for this format anywhere else.
This is very common and well supported as well though:
elem 1
elem 2
elem 3
There are many situations where you need to pass complex data structures around as strings. Serialization, representing arbitrary data structures as strings, solves how to do this.
How does PHP handle serialized data?
"; $test = unserialize[$data];