A common use for base64_decode is processing data without rebuilding the operation manually. Decodes Base64 text back into the original bytes. The strict flag rejects invalid characters.
What Is base64_decode in PHP?
Decodes Base64 text back into the original bytes. The strict flag rejects invalid characters.
Syntax:
base64_decode(?string $string, ?bool $strict = ...) : string|false
Parameters and Return Value
$string— A required string input for this operation. Verify the allowed values in the examples and context below.$strict— An optional bool input for this operation. Verify the allowed values in the examples and context below.
Declared return type: string|false. Distinguish false, an empty result, and exceptions where the API permits them.
Example 1: Basic Usage
This example demonstrates the main operation with a small, controlled input.
<?php
echo base64_decode("SGVsbG8=");
The example focuses on the value produced or the state changed by this API. Review the return value and validate input before using this pattern in production.
Example 2: Another Input
This example changes the input or surrounding setup to illustrate another behavior.
<?php
var_dump(base64_decode("%%bad%%", true));
The example focuses on the value produced or the state changed by this API. Review the return value and validate input before using this pattern in production.
Practical Example
This application-style example shows a self-contained usage pattern and checks its result.
<?php
$encodedInput = "SGVsbG8=";
$decodedInput = base64_decode($encodedInput, true);
if ($decodedInput === false) { throw new InvalidArgumentException("Invalid Base64"); }
echo $decodedInput;
The example focuses on the value produced or the state changed by this API. Review the return value and validate input before using this pattern in production.
Important Behavior and Edge Cases
- Without strict mode, invalid characters may be ignored; never assume Base64 input is trustworthy.
- Use the intended input type and handle empty or malformed values explicitly.
- Where this API requires an extension, version, or file, document that dependency before using it in a real application.
Comparison With Related Functions
| Function or Method | When to Use It |
|---|---|
base64_decode | Decodes Base64 text back into the original bytes. The strict flag rejects invalid characters. |
base64_encode | Encodes binary data as printable Base64 text. Base64 is reversible encoding, not encryption. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Using this API without checking the required input or extension | Without strict mode, invalid characters may be ignored; never assume Base64 input is trustworthy. |
| Treating Base64 as encryption | Use authenticated encryption when confidentiality and integrity are required. |
| Ignoring invalid encoded input | Use strict decoding and verify the result before processing. |
Key Takeaways
- Decodes Base64 text back into the original bytes. The strict flag rejects invalid characters.
- Without strict mode, invalid characters may be ignored; never assume Base64 input is trustworthy.
- Test both the expected case and at least one failure or empty-input case before deployment.
Decodes Base64 text back into the original bytes. The strict flag rejects invalid characters. Use it when the operation fits your input, output and validation requirements.