Cheatsheet: PHP

Cheatsheet: PHP

February 12, 2021

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)

 

 

Leave A Comment