PHP serialization functions serialize () and unserialize () vs. native functions _php Tutorial

Source: Internet
Author: User

PHP serialization functions serialize () and unserialize () vs. native functions


This article mainly introduces PHP serialization functions serialize () and unserialize () and PHP native serialization method comparison, the need for small partners can refer to.

PHP has a good way to format strings and convert an array or object, that is, serialization processing.

There are two ways to serialize variables.

The following example uses the Serialize () and Unserialize () functions:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21st

22

23

24

25

26

27

28

29

30

31

32

33

34

A complex array

$myvar = Array (

' Hello ',

42,

Array (1, ' both '),

' Apple '

);

Convert to a string

$string = serialize ($myvar);

Echo $string;

/* Prints

A:4:{i:0;s:5: "Hello"; I:1;i:42;i:2;a:2:{i:0;i:1;i:1;s:3: "Both";} I:3;s:5: "Apple";}

*/

You can reproduce the original variable

$newvar = Unserialize ($string);

Print_r ($newvar);

/* Prints

Array

(

[0] = = Hello

[1] = 42

[2] = = Array

(

[0] = 1

[1] =

)

[3] = Apple

)

*/

This is the native PHP serialization method.

However, thanks to JSON's popularity in recent years, PHP5.2 has added support for JSON format.

Now you can use the Json_encode () and Json_decode () functions:

?

1

2

3

4

5

6

7

8

9

13 /p>

+

+

/

/

/

+

+

+

+

25

+

+

{

+/

+

+

/

+

A complex array

$myvar = Array (

' Hello ',

42,

Array (1, ' both '),

' Apple '

);

Convert to a string

$string = Json_encode ($myvar);

Echo $string;

/* Prints

["Hello", 42,[1, "I"], "apple"]

*/

You can reproduce the original variable

$newvar = Json_decode ($string);

Print_r ($newvar);

/* Prints

Array

(

[0] = = Hello

[1] = 42

[2] = = Array

(

[0] = 1

[1] =

)

[3] = Apple

)

*/

This will be more effective, especially compatible with many other languages such as JavaScript.

Note: For complex objects, some information may be lost.

The above mentioned is the whole content of this article, I hope you can like.

Note < > : More Exciting tutorials please focus on helping the home program

http://www.bkjia.com/PHPjc/996760.html www.bkjia.com true http://www.bkjia.com/PHPjc/996760.html techarticle PHP serialization functions serialize () and unserialize () vs. native functions This article mainly introduces the PHP serialization function serialize () and unserialize () compared to the native serialization method of PHP, there are ...

  • 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.