PHP 8 reference · String Functions
PHP base64_encode() Function
Encode binary data or text as Base64, using only letters, digits, + and /, so it travels safely as text.
PHP 8 Since 4.0
Syntax
base64_encode(string $string): string
Parameters
| Parameter | Description |
|---|---|
string |
The input string. |
Return value
The Base64 string.
Examples
<?php
echo base64_encode("Hello, World!"), "\n";
Output
SGVsbG8sIFdvcmxkIQ==
<?php
$token = rtrim(strtr(base64_encode(random_bytes(9)), "+/", "-_"), "=");
echo strlen($token), "\n"; // URL-safe random token
Output
12
Version history
| Version | Change |
|---|---|
| PHP 4.0 | Added |
Related
base64_decode()· Decode a Base64 string back to the original data. With strict mode on, invalid input returns false.bin2hex()· Convert binary data to a hexadecimal string, two hex digits per byte. Often used to print random bytes as a token.urlencode()· Encode a string for a URL query value: spaces become + and special characters become %XX codes.- PHP Strings · tutorial