- PHP Basics
- PHP Home
- PHP Environment Setup
- PHP Getting Started
- PHP Basic Syntax
- PHP echo
- PHP print
- PHP echo Vs print
- PHP Comments
- PHP Data Types
- PHP Variables
- PHP Variable Scope
- PHP gettype()
- PHP Constants
- PHP Operators
- PHP Program Control
- PHP Decision Making
- PHP if-elseif-else
- PHP switch
- PHP Loops
- PHP for Loop
- PHP while Loop
- PHP do-while Loop
- PHP foreach Loop
- PHP break & continue
- PHP Popular Topics
- PHP Arrays
- PHP print_r()
- PHP Strings
- PHP Functions
- PHP References
- PHP Object Oriented
- PHP Object Oriented
- PHP Classes & Objects
- PHP Member Variable
- PHP Member Function
- PHP Encapsulation
- PHP Data Abstraction
- PHP Inheritance
- PHP Constructor Destructor
- PHP Polymorphism
- PHP Web Developments
- PHP Web Developments
- PHP GET & POST
- PHP Read Requested Data
- PHP File Handling (I/O)
- PHP File Handling (I/O)
- PHP fopen() | Open File
- PHP Create a File
- PHP fwrite() | Write to File
- PHP fread() | Read File
- PHP feof()
- PHP fgetc()
- PHP fgets()
- PHP fclose() | Close File
- PHP unlink() | Delete File
- PHP Append to File
- PHP copy() | Copy File
- PHP file_get_contents()
- PHP file_put_contents()
- PHP file_exists()
- PHP filesize()
- PHP rename() | Rename File
- PHP fseek()
- PHP ftell()
- PHP rewind()
- PHP disk_free_space()
- PHP disk_total_space()
- PHP mkdir() | Create Directory
- PHP rmdir() | Remove Directory
- PHP glob() | Get Files/Directories
- PHP basename() | Get filename
- PHP dirname() | Get Path
- PHP filemtime()
- PHP file()
- PHP Advanced
- PHP Cookies
- PHP Sessions
- PHP Send Emails
- PHP Serialization
- PHP Namespaces
- PHP File Upload
- PHP Date and Time
- PHP Image Processing
- PHP Regular Expression
- PHP Predefined Variables
- PHP Error Handling
- PHP Debugging
- PHP and MySQLi Tutorial
- PHP and MySQLi Home
- PHP MySQLi Setup
- PHP MySQLi Create DB
- PHP MySQLi Create Table
- PHP MySQLi Connect to DB
- PHP MySQLi Insert Record
- PHP MySQLi Fetch Record
- PHP MySQLi Update Record
- PHP MySQLi Delete Record
- PHP MySQLi SignUp Page
- PHP MySQLi LogIn Page
- PHP MySQLi Store User Data
- PHP MySQLi Close Connection
- PHP connect_errno
- PHP connect_error
- PHP query()
- PHP fetch_row()
- PHP fetch_assoc()
- PHP fetch_array()
- PHP free_result()
- PHP error
- PHP prepare()
- PHP bind_param()
- PHP execute()
- PHP fetch()
- PHP store_result()
- PHP num_rows
- PHP bind_result()
- PHP get_result()
- PHP mysqli_result Class
- PHP Error Constants
- PHP mysqli_driver()
- PHP Misc
- PHP error_reporting()
- PHP Escape Special Characters
- PHP htmlspecialchars()
- PHP new
- PHP header()
- PHP getallheaders()
- PHP empty()
- PHP isset()
- PHP unset()
- PHP exit()
- PHP exit Vs break
- PHP include()
- PHP require()
- PHP include() Vs require()
- PHP AJAX & XML
- PHP AJAX
- PHP XML
- PHP File Handling Functions
- PHP abs()
- PHP Test
- PHP Online Test
- Give Online Test
- All Test List
PHP Serialization
In PHP, serialization is used to store or pass any PHP values around without losing the value's type and structure.
Basically, serialization in PHP generates a storable representation of a PHP value.
PHP serialize() function
The PHP serialize() function is used to serialize any PHP value.
PHP Serialization Example
Let's see some examples given below, of serialization in PHP for different-different PHP type.
Serializing String in PHP
Here is an example demonstrates about serializing a string in PHP.
<html> <head> <title>Serializing a String Value in PHP Example</title> </head> <body> <?php $original_string = "Hello PHP Serialization Tutorial on codescracker.com"; $serialize_string = serialize($original_string); echo $serialize_string; ?> </body> </html>
Here is the sample output that will be shown in your browser after running the above PHP example of serializing a string value.

Serializing Double in PHP
Below is an example of serializing double in PHP.
<html> <head> <title>Serializing a Double Value in PHP Example</title> </head> <body> <?php $original_value = 10.65; $serialized_value = serialize($original_value); echo $serialized_value; ?> </body> </html>
Below is the sample output of above serializing double in PHP example code:

In the same way, you can serialize both Float and Integer value in PHP. Now let's serialize boolean value in PHP using serialize() function.
Serializing Boolean in PHP
Below is an example of serializing boolean value in PHP.
<html> <head> <title>Serializing Boolean Value in PHP Example</title> </head> <body> <?php // let's first check for true (boolean value) $orig_bool_val = true; $serialize_bool_val = serialize($orig_bool_val); echo $serialize_bool_val; echo "<hr/>"; // now let's check for false (boolean value) $orig_bool_val = false; $serialize_bool_val = serialize($orig_bool_val); echo $serialize_bool_val; ?> </body> </html>
Below is the sample output produced by the above serializing boolean value example code in PHP.

Serializing Null in PHP
Let's serialize null value in PHP using the following example code:
<html> <head> <title>Serializing Null in PHP Example</title> </head> <body> <?php $orig_null_val = null; $serialize_null_val = serialize($orig_null_val); echo $serialize_null_val; ?> </body> </html>
Following is the sample output of the above example code of serializing null in PHP.

Serializing Array in PHP
Let's serialize an array in PHP with the help of following example code:
<html> <head> <title>Serializing an Array in PHP Example</title> </head> <body> <?php $orig_array_val = array( 10, 10.26, 'Array Serialization', 'boolean'=>false, null ); $serialize_array_val = serialize($orig_array_val); echo $serialize_array_val; ?> </body> </html>
Here is the sample output of the above serializing an array value in PHP example code:

Serializing Object in PHP
Now let's serialize an object in PHP using the following example code:
<html> <head> <title>Serializing an Object in PHP Example</title> </head> <body> <?php class className { var $varName = 20; function functionName() { return 'return value'; } } $orig_object = new className(); $serialize_object = serialize($orig_object); echo $serialize_object; ?> </body> </html>
Below is the sample output produce by the above example code of serializing an object in PHP.

« Previous Tutorial Next Tutorial »
Follow/Like Us on Facebook
Subscribe Us on YouTube