base64_encode
Encodes data as text made of ASCII letters and digits.
Syntax
base64_encode(string $string): stringParameters
| Parameter | Type | Description |
|---|---|---|
| $string | string | The source data. |
Return value
stringA Base64 string.
Examples
Encoding and decoding:
<?php
$encoded = base64_encode('Hello, world!');
echo $encoded, "\n";
echo base64_decode($encoded);Output:
SGVsbG8sIHdvcmxkIQ==
Hello, world!Notes
Base64 is not encryption: the original data is restored with one function. It carries binary data where only text is allowed: in JSON, emails, URLs.
See also
- urlencode — Encodes a string for use in a URL.
- random_bytes — Returns cryptographically secure random bytes.
In the tutorial
- HTTP requests and responses — How PHP receives a request and builds a response — the request method and address, headers, status codes, redirects and JSON responses.