PHP base64_encode() Function
The PHP base64_encode() function encodes the given string with MIME (Multipurpose Internet Mail Extensions) base64. This encoding is designed to make binary data survive transport through transport layers that are not 8-bit clean, such as mail bodies. Base64-encoded data takes about 33% more space than the original data.
Syntax
base64_encode(string)
Parameters
string |
Required. Specify the string to encode. |
Return Value
Returns the encoded data, as a string.
Example:
The example below shows the usage of base64_encode() function.
<?php $str = "AlphaCodingSkills"; $encoded_str = base64_encode($str); //displaying the encoded string echo $encoded_str; ?>
The output of the above code will be:
QWxwaGFDb2RpbmdTa2lsbHM=
Example:
Consider one more example which shows how a string is encoded and decoded with MIME base64.
<?php $str1 = "Programming is fun"; $encoded_str1 = base64_encode($str1); echo "The string is: $str1 \n"; echo "Encoded string is: $encoded_str1 \n"; $str2 = "UHJvZ3JhbW1pbmcgaXMgZnVu"; $decoded_str2 = base64_decode($str2); echo "\nThe string is: $str2 \n"; echo "Decoded string is: $decoded_str2 \n"; ?>
The output of the above code will be:
The string is: Programming is fun Encoded string is: UHJvZ3JhbW1pbmcgaXMgZnVu The string is: UHJvZ3JhbW1pbmcgaXMgZnVu Decoded string is: Programming is fun
❮ PHP URLs Reference