PHP base64_decode() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
URL

Definition

In PHP, base64_decode() turns a base64 string back into the original data.

Pass true as the second argument to reject input that contains characters outside the base64 alphabet.

Syntax

Call base64_decode() with the values you need. Start with this line.

PHP
$text = base64_decode($encoded);
CallWhat it does
base64_decode("SGVsbG8=")Returns "Hello".
base64_decode("###", true)Returns false (strict mode).

Without strict mode, characters outside the base64 alphabet are skipped silently.

Library declarations
PHP
base64_decode(string $string, bool $strict = false): string|false

Parameters

ParameterDescription
stringRequired. The base64 text to decode.
strictOptional. If true, the function returns false when the input has characters outside the base64 alphabet. Default is false.

Return Value

Return ValueDescription
string|falseThe decoded data. In strict mode it returns false if the input is invalid.

PHP Examples

Example 1 — Decode a Base64 String

Turn a base64 string back into the original text.

PHP
<?php
$encoded = "SGVsbG8gV29ybGQ=";

echo base64_decode($encoded), PHP_EOL;

How It Works

  1. Start with base64 text. It was made with base64_encode().
  2. Call base64_decode(). Each group of 4 characters becomes 3 bytes.
  3. Get the original data. Here it is the text Hello World.
  4. Print the result. echo shows it with a new line.

Example 2 — Use Strict Mode

A bad character is skipped by default, but strict mode rejects it.

PHP
<?php
$bad = "SGVs*bG8=";

var_dump(base64_decode($bad));
var_dump(base64_decode($bad, true));

How It Works

  1. Add a bad character. The * is not part of the base64 alphabet.
  2. Decode in normal mode. The * is skipped, so you still get Hello.
  3. Decode in strict mode. The same input now returns false.
  4. Pick strict for user input. It tells you the data is not valid base64.

Example 3 — Decode a URL-Safe Token

URL-safe base64 uses - and _ instead of + and /. Swap them back first.

PHP
<?php
$token = "SGVsbG8sIFBIUCE";

$base64 = strtr($token, "-_", "+/");

echo base64_decode($base64), PHP_EOL;

How It Works

  1. Start with a URL-safe token. It has no padding and may use - and _.
  2. Swap the characters. strtr() changes - to + and _ to /.
  3. Decode as usual. PHP accepts base64 without the = padding.
  4. Print the text. You get Hello, PHP!.

Frequently Asked Questions

base64_decode() converts a base64 string back into the original data. It is the reverse of base64_encode().
base64_decode(string $string, bool $strict = false): string|false. Pass the base64 text and, if you like, true for strict mode.
In strict mode, base64_decode() returns false if the input contains characters outside the base64 alphabet. Without it, those characters are ignored.
No. Base64 only changes how data looks so it can travel as text. Anyone can decode it, so never use it to hide passwords or secrets.
The original data may be binary, such as an image or a file. Save it to a file or process it as bytes instead of printing it.

Next: base64_encode()

base64_encode() encodes data as a base64 string.

base64_encode() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful