Skip to main content

How to Decode Base64 in PHP with base64_decode

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

Base64 turns bytes into plain text so they can travel in JSON, URLs or environment variables. PHP's base64_decode turns it back, and with its second argument set to true it refuses anything that isn't valid base64 instead of quietly skipping it.

Syntax​

Syntax
$<data> = base64_decode($<text>, true);
PartWhat it does
base64_decodeDecodes base64 text and returns the bytes as a string.
trueStrict mode: returns false for characters outside the base64 alphabet.
$<data>The decoded bytes, or false when the input was invalid.

The PHP manual for base64_decode: "If the strict parameter is set to true then the base64_decode() function will return false if the input contains character from outside the base64 alphabet. Otherwise invalid characters will be silently discarded."

The Example​

The example decodes SGVsbG8=, the base64 form of Hello, then a version with an * in it, both in strict mode.

Prerequisites​

  • PHP for the command line. This guide uses PHP 8.6.0.

Step 1: Write the Code​

Create a file named main.php:

main.php, which decodes SGVsbG8= with base64_decode in strict mode and var_dumps it, then var_dumps base64_decode of SGV*bG8= in strict mode.

main.php
<?php

$data = base64_decode('SGVsbG8=', true);
var_dump($data);

var_dump(base64_decode('SGV*bG8=', true));

Step 2: Run It​

Terminal
php main.php
Output
string(5) "Hello"
bool(false)

A zsh terminal where php main.php prints string(5) "Hello" and bool(false).

The valid text decoded to the five bytes of Hello. The * made strict mode return false. The command is the same in zsh, bash and PowerShell.

Without Strict Mode​

Leave out true and invalid characters are dropped without a word: base64_decode('SGV*sbG8=') returned "Hello". That hides broken input, so always pass true and check for false.

Base64url and the Way Back​

Base64url, used in JWTs and URLs, writes - and _ where base64 writes + and /. Swap them back before decoding: base64_decode(strtr('-_8', '-_', '+/'), true) returned the two bytes 251 and 255. Missing = padding was accepted: base64_decode('SGVsbG8', true) still returned "Hello". Encoding is base64_encode('Hello'), which returned SGVsbG8=.

Common Mistakes​

MistakeWhat happensDo this instead
Calling it without trueInvalid characters are silently discardedPass true
Comparing the result with == falseAn empty string or "0" also matchesCompare with === false
Decoding base64url as isStrict mode returns false for - and _Swap them with strtr first

Troubleshooting​

SymptomLikely causeFix
base64_decode returns falseA character outside the alphabet, such as *, -, _ or a spaceClean the input, or convert base64url with strtr
Garbled textThe bytes aren't text, such as a keyKeep them as a binary string

FAQ​

Is the result always text?​

No. The manual notes that "The returned data may be binary." A 32-byte key decodes to 32 bytes that aren't printable.

Does strict mode require = padding?​

No: in my check, strict mode decoded SGVsbG8 without its =.

Conclusion​

base64_decode($text, true) decodes base64 and returns false for invalid input; check for it with ===. Swap - and _ with strtr to decode base64url.

Additional Resources​