This tutorial will show you how to encode and decode a string in base64 format in PHP.
Base64 is a useful encoding type that allows you to encode a string in PHP that you can easily decode.
PHP uses the Base64 MIME standard for its encoding. This standard was originally designed to help send binary data in text-based protocols, such as e-mail.
Something like an image would be encoded in this format during transit and then decoded back to its original data by the receiving customer.
There are many other uses for Base64 encoding and decoding in PHP. For example, in a previous comment system, we used Base64-encoded SVG data, which was then interpreted by the web browser back to its original data.
In the next two sections, we’ll show you how to encode and decode using Base64 in PHP. In particular, we will touch
base64_decode() PHP functions.
Please note that Base64 should never be used for security. It is designed to be efficiently encoded and decoded on the go and provides no protection.
- 1 The base64_encode () function in PHP
- 2 The base64_decode () function in PHP
- 3 Conclusion
The base64_encode () function in PHP
In this section, we’ll show you how to encode a Base64 format string in PHP. To do this, you’ll learn all about base64_encode ().
PHP base64_encode () function definition.
Let’s show you exactly how the base64_encode () function is defined in PHP. Knowing how it is defined will make it easier for you to understand what kind of data you will be working with.
This function is very simple because it has only one singular parameter waiting for a string.
Below you can see how
base64_encode() is defined. This definition shows the parameters the expected data type and function return type.
This function requires only one parameter. This parameter is the string you want to encode according to the Base64 MIME standard.
PHP will return a Base64-encoded version of your string. You will notice that this string will consume more data and will be longer than the original data.
Due to the way Base64 works, it will consume about 33% more data than the original string, so be careful if you are dealing with a large source string.
Example of using base64_encode ()
Now that we know the definition of the base64_encode () function in PHP, we can explore an example of how to use it in your code.
We will keep this example relatively simple by passing a string directly into operation.
At the top of this example, we'll create a variable called "
$website"And assign the string"
Then we use PHPs
base64_encode() function, moving to "
$website”Variable in a single parameter. The returned Base64 string will be removed thanks to us using the echo statement.
Below you can see how PHP encoded "
pimylifeup.com”String in Base64 format.
You will immediately notice that the resulting string is longer and ends with an equal sign. This is because the MIME-Base64 standard uses the equal sign for the filler, because the length must always be a multiple of three.
The base64_decode () function in PHP
Now that you know how to encode a string in MIME-Base64 format in PHP, let us explore how to decode the string.
PHP provides a function called
base64_decode() which will take a coded string and decode it back to its original data.
PHP base64_decode () function definition.
Let's move on to the definition
base64_decode() within PHP. This definition will show you the different parameters and the type of return of this function.
This function is what will take a Base64 string and convert it back to its original data.
Below you can see the function definition. This shows the two parameters along with the type of data returned.
This feature has two arguments that you can use. The first is mandatory and the second is optional.
$string- This parameter it's necessary and this is where you will switch to your Base64 MIME encoded string.
If your string uses a different standard, you may have trouble using this feature.
$strict- Strict parameter is optional and is set to "
true"PHP will return"
false”If the Base64 string contains any characters that are not available in the Base64 alphabet.
When set to "
false", PHP will silently remove any invalid characters and try to decode the resulting string.
base64_decode() the function will return "
false”To failure or decoded data. Decoded data can be binary, so be prepared to handle this in your code.
Examples of using base64_decode () in PHP
In this section, we'll show you how to use it
base64_decode() function in PHP. We will give two different examples below.
The first example will show how the function is used without the optional parameter. The second example will show you how the strict option works.
Basic use of the base64_decode () function.
Our first example will explore the basic use of
base64_decode() function. So with this example, we'll ignore the second parameter.
To start this script, we will define a PHP string called "
$string”And assign it a Base64 encoded string (
"cGlteWxpZmV1cC5jb20="). This string is the same one I coded earlier in this guide.
Then we move on. "
$string”Variable directly in
base64_decode() function. Then we remove the resulting string using the echo statement in front of it.
After running the code example above, PHP will decode our Base64 string back to its original data. In our case, this will be the following text.
Using the strict field in the base64_decode () function.
Now that we've seen the basic usage, let's try to use it
base64_decode() function, but we will activate the strict mode by setting the second parameter to "
We'll adjust our encoded string by adding three invalid Base64 characters to show what it does. These are exclamation marks (
!), a question mark (
?), and a hashtag (
At the top of our script, we define our variable called "
$base64_string"And assign it the value"
We will then pass this on to two separate function calls.
- The first one will use the PHP base64_decode () function but will leave it strictly disabled.
This will show how PHP normally handles a Base64 string that contains invalid characters.
- We enable the strict functionality of base64_decode () for the second call by setting the second parameter to
Instead of trying to remove invalid characters, the function will fail and return
For both functions, we pass their result directly to the PHP var_dump () function. This function will print both the value and the type of data returned.
Below you can see how PHP works
base64_decode() the function works when setting the strict parameter. When not strictly set or set to false, it will clean the supplied string and try to decode it.
However, it will simply fail and return
false when strict mode is activated.
In this tutorial, we've shown you how PHP handles Base64 encoded strings. You can easily decode and encode any given string using two simple functions.
Although originally designed to help send binary data in text-optimized protocols, this encoding still has its uses.
If you have questions about
base64_decode() functions, please comment below.
Be sure to check out the other PHP tutorials if you want to learn more. We have many other programming guides if you want to learn a new language.