On this page
Cheatsheet: PHP
When working with PHP, handling data efficiently is a common requirement—whether you’re storing values in a database, sending data over a network, or exchanging information with APIs. PHP provides several built-in functions to encode, decode, format, and transform data into different representations. In this article, we’ll explore some commonly used PHP functions such as serialize(), unserialize(), base64_encode(), json_encode(), json_decode(), and date() with simple examples to help you understand when and how to use them effectively.
serialize():
Convert a storable representation of a value. The serialize() function converts a storable representation of a value. To serialize data means to convert a value to a sequence of bits so that it can be stored in a file, a memory buffer, or transmitted across a network.
<?php
$colors = array("Red", "Green", "Blue");
$serialized_colors = serialize($colors);
echo $serialized_colors;
//OUTPUT: a:3:{i:0;s:3:"Red";i:1;s:5:"Green";i:2;s:4:"Blue";}
?>
unserialize():
Convert serialized data back into actual data:
<?php
$serialized_colors = 'a:3:{i:0;s:3:"Red";i:1;s:5:"Green";i:2;s:4:"Blue";}';
$unserialized_colors = unserialize($serialized_colors);
print_r($unserialized_colors);
//OUTPUT: Array ( [0] => Red [1] => Green [2] => Blue )
?>
base64_encode():
Encodes data with MIME base64
<?php $str = 'Coding is fun...'; echo base64_encode($str); //OUTPUT: Q29kaW5nIGlzIGZ1bi4uLg== ?>
json_decode()
The json_decode() function is used to decode or convert a JSON object to a PHP object.
$result = json_decode($jsondata, true); // Return arrry $result = json_decode($jsondata); // Return object
json_encode()
The json_encode() function is used to encode a value to JSON format.
date()
The date() function formats a local date and time, and returns the formatted date string. The date function should have at least one parameter.
date(format, timestamp)
- END -



