How to Use the PHP base64_decode() Function

09/09/2021

Contents

In this article, you will learn how to use the PHP base64_decode() function.

PHP base64_decode() Function

The base64_decode() function is a built-in function in PHP and is used to decode data encoded with the base64_encode() function.

Syntax:
string base64_decode ( string $data [, bool $strict = false ] )
Parameters:
  • $data: The encoded data that you want to decode.
  • $strict (optional): A Boolean value that indicates whether the base64_decode() function should return an error if the input contains characters outside the base64 alphabet. By default, $strict is set to false.
Example:
<?php
  $encoded_data = 'SGVsbG8gV29ybGQh';
  $decoded_data = base64_decode($encoded_data);

  echo $decoded_data; 
?>

The output of this code will be: “Hello World!”

The base64_decode() function takes an encoded string as input and returns its decoded version. The encoding is performed using the base64_encode() function, which takes a string and returns a base64-encoded version of that string.

Base64 encoding is a way of representing binary data (such as images, audio, or other files) in ASCII format, so that it can be transmitted over text-based protocols such as HTTP and SMTP. The encoding process replaces each group of three bytes in the original data with four ASCII characters, resulting in a 4:3 ratio of encoded to original data.

In PHP, the base64_decode() function can be used to decode data that has been encoded with base64_encode(). The function takes a single argument, the encoded data, and returns the decoded version of that data as a string.

It is important to note that base64 encoding does not provide any encryption or security for the data. It simply provides a way to represent binary data as text. If you need to transmit sensitive data, you should consider using encryption techniques in addition to base64 encoding.