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

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

base64_encode(string $string): string

Parameters

ParameterDescription
string The input string.

Return value

The Base64 string.

Examples

Encode textTry it yourself
<?php
echo base64_encode("Hello, World!"), "\n";

Output

SGVsbG8sIFdvcmxkIQ==
A URL-safe tokenTry it yourself
<?php
$token = rtrim(strtr(base64_encode(random_bytes(9)), "+/", "-_"), "=");
echo strlen($token), "\n"; // URL-safe random token

Output

12

Version history

VersionChange
PHP 4.0Added

Was this page helpful?