Php function serialize () and unserialize () usage example, sprintf function usage _ PHP Tutorial

Source: Internet
Author: User
Php functions serialize () and unserialize () usage instances, sprintf usage. Php functions serialize () and unserialize () usage examples. sprintf usage this article describes the php functions serialize () and unserialize () usage. Share it with you for your reference. Example of usage of php functions serialize () and unserialize () and sprintf

This example describes the usage of php functions serialize () and unserialize. Share it with you for your reference. The specific method is as follows:

This example describes the php functions serialize () and unserialize. To change serialized strings back to PHP values, use unserialize (). Serialize () can process any type other than resource. Even serialize () contains arrays that point to its own reference. References in arrays/objects of serialize () will also be stored.

Serialize () returns a string that contains a byte stream that represents value and can be stored anywhere. This facilitates storing or passing PHP values without losing their types and structures.

To change serialized strings back to PHP values, use unserialize (). Serialize () can process any type other than resource. Even serialize () contains arrays that point to its own reference. References in arrays/objects of serialize () will also be stored.

When an object is serialized, PHP tries to call the member function _ sleep () of the object before the sequential action (). This allows objects to be cleared before being serialized. Similarly, when unserialize () is used to restore an object, the _ wakeup () member function is called.

Note: in PHP 3, object attributes will be serialized, but methods will be lost. PHP 4 breaks this restriction and can store attributes and methods at the same time. For more information, see the serialization object section of the class and object.
Serialize () and unserialize () are described in the php manual as follows:

Serialize-Generates a storable representation of a value
Serialize-generate a representation of a stored value
Unserialize-Creates a PHP value from a stored representation
Unserialize-create a PHP value from a stored representation
Serialize, translated as "serialization, continuous", usually called "Serialization"

This function is useful, especially when used with unserialize.
I think it is useful to store data in a database or record it in a file.
Of course, this type of data must be complex (neither complicated nor serialize is required, and I think at least it must be an array) and be non-"index or primary key" in the database ", of course, it is best to leave this database field irrelevant to any search program in the system. of course, the data after serialize can still be searched because the specific data is not encrypted or changed.

The code is as follows:

<? Php
// Simple
$ Array = array ();
$ Array ['key'] = 'Website ';
$ Array ['value'] = 'www .isoji.org ';
$ A = serialize ($ array );
Echo $;
Unset ($ array );
$ A = unserialize ($ );
Print_r ($ );

// Declare a class
Class dog {
Var $ name;
Var $ age;
Var $ owner;
Function dog ($ in_name = "unnamed", $ in_age = "0", $ in_owner = "unknown "){
$ This-> name = $ in_name;
$ This-> age = $ in_age;
$ This-> owner = $ in_owner;
}
Function getage (){
Return ($ this-> age * 365 );
}

Function getowner (){
Return ($ this-> owner );
}

Function getname (){
Return ($ this-> name );
}
}
// Instantiate this class
$ Ourfirstdog = new dog ("Rover", 12, "Lisa and Graham ");
// Use the serialize function to convert this instance into a serialized string
$ Dogdisc = serialize ($ ourfirstdog );
Print $ dogdisc; // $ ourfirstdog has been serialized as a string O: 3: "dog": 3: {s: 4: "name"; s: 5: "Rover "; s: 3: "age"; I: 12; s: 5: "owner"; s: 15: "Lisa and Graham ";}
Print'
';
/*
Here you can store the string $ dogdisc anywhere, such as session, cookie, database, and PHP files.
*/
// Cancel this class here
Unset ($ ourfirstdog );
/* Restore operation */
/*
Here, you can read the string $ dogdisc from your storage location, such as session, cookie, database, and PHP files.
*/

// Here we use unserialize () to restore serialized objects.
$ Pet = unserialize ($ dogdisc); // $ pet is the previous $ ourfirstdog object.
// Get the age and name attributes
$ Old = $ pet-> getage ();
$ Name = $ pet-> getname ();
// This class can be used without instantiation at this time, and both attributes and values remain in the state before serialization.
Print "Our first dog is called $ name and is $ old days old
";
Print'
';
?>

I hope this article will help you with PHP programming.


Write the php variable serialization and deserialization functions, and give an example of an application.

"; Print_r (unserialize ($ new);?> Serialize () encodes variables and their values into text form
Unserialize () restore original variable


Question about the php serialize () and unserialize () methods?

$ Person1 = new ysr (25, 'yunshao ');
$ Person1 is an object. after serializing it, it is just another way of expression!
$ Person2 = unserialize ($ listInfo );
After this object is restored, it is an object!
This object already has the getAge () and getName ()
Operation method, you can use these methods!

Examples of usage of serialize () and unserialize (), sprintf usage this article describes the use of php serialize () and unserialize. Share it with you for your reference. Details...

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.