PHP Base64_decode Function

Sarwan Soomro Jan 30, 2022
  1. PHP Base64_decode Function Example Code 1
  2. PHP Base64_decode Function Example Code 2
PHP Base64_decode Function

PHP base64_decode function converts strings into long plain text, which are then sent along with the main function of user-defined value sets.

base64_decode() is a php’s inbuilt function used most commonly used in mailing features over the web.

Function syntax:

string base64_decode( $string, $strict)

It takes two parameters.

  1. $string - It is used for storing the encoded data, and it is mandatory.
  2. $strict - Although this parameter is not mandatory as the former, when it is set TRUE, the base64_decode returns FALSE provided its input contains data other than the function alphabet. Otherwise, the invalid data is automatically discarded.

Return values:

The base64 decode function returns FALSE in case of a failure and might return binary values.

PHP Base64_decode Function Example Code 1

<?php
    $string = base64_encode ('Your string values are encoded');
    echo "1.Here is the encoded function value in a machine readable format = ".$string ."<br>";
    echo "2.The function decodes the formerly encoded string value" ."<br>" .base64_decode($string);
?>

Output:

1.Here is the encoded function value in a machine readable 
format = WW91ciBzdHJpbmcgdmFsdWVzIGFyZSBlbmNvZGVk
2. The function decodes the formerly encoded string value
Your string values are encoded.

The example code shows how the base64_decode() function works in a simple scenario.

In order to decode the encoded data, the program assigns a value to a variable, which is then used along with the decode function.

While the output indicates how base64_decode() converts the data back to user readable format.

PHP Base64_decode Function Example Code 2

<?php
//The following variable is assigned with a string set 
    $string = "HELLO--こんにちは--你好"; 
//string contains bilingual text 
//base64 is used to encode the data first into the $enco variable
    $enco = base64_encode ($string);
//finally the base64_decode functionis used to decode the encoded value 
    $deco = base64_decode ($enco); 
    echo $deco; 
?>

Output:

HELLO--こんにちは--你好
Sarwan Soomro avatar Sarwan Soomro avatar

Sarwan Soomro is a freelance software engineer and an expert technical writer who loves writing and coding. He has 5 years of web development and 3 years of professional writing experience, and an MSs in computer science. In addition, he has numerous professional qualifications in the cloud, database, desktop, and online technologies. And has developed multi-technology programming guides for beginners and published many tech articles.

LinkedIn

Related Article - PHP Encoding