PHP Base64_decode Function
- Understanding Base64 Encoding
- Example 1: Basic Usage of base64_decode
- Example 2: Decoding Base64 Images

Base64 encoding is a popular method for encoding binary data into ASCII string format, making it easier to transmit over media that are designed to handle text. In PHP, the base64_decode
function plays a vital role in decoding such encoded data.
This tutorial aims to provide a clear understanding of how the base64_decode
function works in PHP, illustrated through two straightforward examples. Whether you are a beginner or an experienced developer, this guide will help you grasp the concept of base64 decoding in PHP, enhancing your coding skills and knowledge.
Understanding Base64 Encoding
Base64 encoding is often used in data transmission to ensure that binary data remains intact without modification during transport. This is particularly useful when dealing with email attachments or data embedded in URLs. The base64_decode
function in PHP allows you to revert this process, converting the encoded string back into its original binary format.
The function takes a base64 encoded string as input and returns the decoded data. This is essential when you need to retrieve original data from a base64 encoded format, such as images or files.
Example 1: Basic Usage of base64_decode
Let’s start with a simple example to demonstrate how to use the base64_decode
function. In this example, we will decode a base64 encoded string.
<?php
$encodedString = "SGVsbG8sIFdvcmxkIQ==";
$decodedString = base64_decode($encodedString);
echo $decodedString;
?>
Output:
Hello, World!
In this example, we have an encoded string “SGVsbG8sIFdvcmxkIQ==”. This string is a base64 representation of “Hello, World!”. By using the base64_decode
function, we decode the string back to its original form. The result is then printed out, displaying “Hello, World!” on the screen. This basic example clearly shows how straightforward it is to decode base64 strings in PHP.
Example 2: Decoding Base64 Images
Now, let’s take a look at a more practical example where we decode a base64 encoded image. This is a common use case, especially when working with image uploads or data transfer.
<?php
$encodedImage = "iVBORw0KGgoAAAANSUhEUgAAAAUA
AAAAFCAIAAAxWl3bAAAAA3NCSVQICAjb4U/gAAAACXBIWXMAAB7CAAAewgFu0HU+AAABHklEQVR42mJ0gG9wYXYgA4QWnQKQy4YgMDAwMDIyMDQw8u4B4u4B4u4B4u4B4u4B4u4B4u4B4u4B4u4B4u4B4u4B4u4B4u4B4u4B4u4B4u4B4u4B4u4B4u4B4u4B4u4B4u4B4u4B4u4B4u4B4u4B4u4B4u4B4u4B4u4B4u4B4u4B4u4B4u4B4u4B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4u4B4u4B4u4B4u4B4u4B4u4B4u4B4u4B4u4B4u4B4u4B4u4B4u4B4u4B4u4B4u4B4u4B4u4B4u4B4u4B4u4B4u4B4u4B4u4B4u4B4u4B4u4B4u4B4u4B4u4B4u4B4u4B4u4B4u4B4u4B4u4B4u4B4u4B4u4B4u4B4u4B4u4B4u4B4u4B4u4B4u4B4u4B4u4B4u4B4u4B4u4B4u4B4u4B4u4B4
Sarwan Soomro is a freelance software engineer and an expert technical writer who loves writing and coding. He has 5 years of web development and 3 years of professional writing experience, and an MSs in computer science. In addition, he has numerous professional qualifications in the cloud, database, desktop, and online technologies. And has developed multi-technology programming guides for beginners and published many tech articles.
LinkedIn