PHP base64_encode() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
URL

Definition

In PHP, base64_encode() turns data into a plain-text string made only of letters, digits, +, / and =.

It is handy when binary data must travel through text, such as in an email, a JSON file, or a data URI.

Syntax

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

PHP
$encoded = base64_encode($data);
CallWhat it does
base64_encode("Hello")Returns "SGVsbG8=".
base64_encode("A")Returns "QQ==".

The result is about 33% longer than the original data.

Library declarations
PHP
base64_encode(string $string): string

Parameters

ParameterDescription
stringRequired. The data to encode.

Return Value

Return ValueDescription
stringThe base64 encoded text.

PHP Examples

Example 1 — Encode a String

Convert a short text into base64.

PHP
<?php
$text = "Hello World";

echo base64_encode($text), PHP_EOL;

How It Works

  1. Start with a string. Any bytes work, not just text.
  2. Call base64_encode(). Every 3 bytes become 4 characters.
  3. Get a safe text string. It uses only letters, digits, +, / and =.
  4. Print the result. You can decode it later with base64_decode().

Example 2 — See How Padding Works

The = sign pads the end when the data is not a multiple of 3 bytes.

PHP
<?php
echo base64_encode("A"), PHP_EOL;
echo base64_encode("AB"), PHP_EOL;
echo base64_encode("ABC"), PHP_EOL;

How It Works

  1. Encode 1 byte. It needs two = signs to fill the last group.
  2. Encode 2 bytes. It needs one = sign.
  3. Encode 3 bytes. They fit one full group, so there is no padding.
  4. Remember the rule. Padding only fills the last group of 4 characters.

Example 3 — Make a Data URI and a URL-Safe Token

Build a data URI, then change the result so it is safe to place in a URL.

PHP
<?php
echo "data:text/plain;base64," . base64_encode("Hi"), PHP_EOL;

$token = base64_encode("Hello?>");
$urlSafe = strtr(rtrim($token, "="), "+/", "-_");

echo $token, PHP_EOL;
echo $urlSafe, PHP_EOL;

How It Works

  1. Build a data URI. Put the base64 text after ;base64,.
  2. Encode text with special bytes. The result can contain / and =.
  3. Remove the padding. rtrim() cuts the trailing = signs.
  4. Swap two characters. strtr() turns + into - and / into _.

Frequently Asked Questions

base64_encode() converts data into a base64 text string so it can be sent or stored where only plain text is allowed.
base64_encode(string $string): string. Pass the data to encode and it returns the base64 text.
The = sign is padding. It fills the last group of 4 characters when the input length is not a multiple of 3.
The normal output may contain + and /, which have special meaning in URLs. Replace them with - and _, or run the result through rawurlencode().
No. Base64 is only an encoding, not encryption. Use password_hash() to store passwords.

Next: get_headers()

get_headers() fetches the response headers a server sends for a URL.

get_headers() 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