PHP Function Reference

PHP base64_decode() Function



The PHP base64_decode() function decodes the given string encoded with MIME (Multipurpose Internet Mail Extensions) base64.

Syntax

base64_decode(string, strict)

Parameters

string Required. Specify the encoded string.
strict Optional. If set to true then the function returns false if the string contains any character outside the base64 alphabet. Otherwise invalid characters are silently discarded. Default is false.

Return Value

Returns the decoded data or false on failure. The returned data may be binary.

Example:

The example below shows the usage of base64_decode() function.

<?php
$str = "QWxwaGFDb2RpbmdTa2lsbHM=";
$decoded_str = base64_decode($str);

//displaying the decoded string
echo $decoded_str;
?>

The output of the above code will be:

AlphaCodingSkills

Example:

Consider one more example which shows how a string is encoded and decoded with MIME base64.

<?php
$str1 = "Programming is fun";
$encoded_str1 = base64_encode($str1);

echo "The string is: $str1 \n";
echo "Encoded string is: $encoded_str1 \n";

$str2 = "UHJvZ3JhbW1pbmcgaXMgZnVu";
$decoded_str2 = base64_decode($str2);

echo "\nThe string is: $str2 \n";
echo "Decoded string is: $decoded_str2 \n";
?>

The output of the above code will be:

The string is: Programming is fun 
Encoded string is: UHJvZ3JhbW1pbmcgaXMgZnVu 

The string is: UHJvZ3JhbW1pbmcgaXMgZnVu 
Decoded string is: Programming is fun 

❮ PHP URLs Reference