Home >Backend Development >PHP Tutorial >PHP Base64_decode function

PHP Base64_decode function

WBOY
WBOYforward
2024-02-28 08:10:36974browse

PHP Base64_decode function is an important function in PHP programming for decoding Base64 encoded strings. Through the Base64_decode function, the Base64-encoded string can be decoded into original data, which is convenient for use during data transmission and storage. This article will introduce the basic syntax and usage of the Base64_decode function to help readers better understand and use this function. If you are interested in PHP programming and data processing, then you may wish to continue reading to learn more about the PHP Base64_decode function.

base64_decode() is a built-in function of php, which is most commonly used for the email function on the network.

Function syntax:

<code><code class="language-php hljs" data-lang="php"><span style="display:flex;"><span>string base64_decode( <span style="color:#19177c">$string</span>, <span style="color:#19177c">$strict</span>)
</span></span></code></code>

It requires two parameters.

  1. $string - Used to store encoded data, required.
  2. $strict - Although this parameter is not mandatory like the former, when it is set to TRUE,, base64_decode will return FALSE, Provided that its input contains data outside the function's alphabet. Otherwise, invalid data will be automatically discarded.

Return value:

base64 The decoding function returns FALSE in case of failure, and may return a binary value.


PHP Base64_decode function example code 1

<code><code class="language-php hljs" data-lang="php"><span style="display:flex;"><span><span style="color:#666"><?</span>php
</span></span><span style="display:flex;"><span><span style="color:#19177c">$string</span> <span style="color:#666">=</span> base64_encode (<span style="color:#ba2121">'Your string values are encoded'</span>);
</span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">echo</span> <span style="color:#ba2121">"1.Here is the encoded funct<strong class="keylink">io</strong>n value in a <strong class="keylink">Mac</strong>hine readable f<strong class="keylink">ORM</strong>at = "</span><span style="color:#666">.</span><span style="color:#19177c">$string</span> <span style="color:#666">.</span><span style="color:#ba2121">"<br>"</span>;
</span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">echo</span> <span style="color:#ba2121">"2.The function decodes the formerly encoded string value"</span> <span style="color:#666">.</span><span style="color:#ba2121">"<br>"</span> <span style="color:#666">.</span>base64_decode(<span style="color:#19177c">$string</span>);
</span></span><span style="display:flex;"><span><span style="color:#bc7a00">?></span><span >
</span></span></span></code></code>

Output:

<code><code class="language-text hljs" data-lang="text"><span style="display:flex;"><span>1.Here is the encoded function value in a machine readable 
</span></span><span style="display:flex;"><span>format = WW91ciBzdHJpbmcgdmFsdWVzIGFyZSBlbmNvZGVk
</span></span><span style="display:flex;"><span>2. The function decodes the formerly encoded string value
</span></span><span style="display:flex;"><span>Your string values are encoded.
</span></span></code></code>

The example code shows how the base64_decode() function can be used in a simple Work in the scene.

To decode encoded data, the program assigns a value to a variable and then uses it with the decoding function.

And the output shows base64_decode() how to convert the data back to a user-readable format.


PHP Base64_decode function sample code 2

<code><code class="language-php hljs" data-lang="php"><span style="display:flex;"><span><span style="color:#666"><?</span>php
</span></span><span style="display:flex;"><span><span style="color:#408080;font-style:italic">//The following variable is assigned with a string set 
</span></span></span><span style="display:flex;"><span><span style="color:#408080;font-style:italic"></span><span style="color:#19177c">$string</span> <span style="color:#666">=</span> <span style="color:#ba2121">"HELLO--こんにちは--你好"</span>; 
</span></span><span style="display:flex;"><span><span style="color:#408080;font-style:italic">//string cont<strong class="keylink">ai</strong>ns bilingual text 
</span></span></span><span style="display:flex;"><span><span style="color:#408080;font-style:italic">//base64 is used to encode the data first into the $enco variable
</span></span></span><span style="display:flex;"><span><span style="color:#408080;font-style:italic"></span><span style="color:#19177c">$enco</span> <span style="color:#666">=</span> base64_encode (<span style="color:#19177c">$string</span>);
</span></span><span style="display:flex;"><span><span style="color:#408080;font-style:italic">//finally the base64_decode functionis used to decode the encoded value 
</span></span></span><span style="display:flex;"><span><span style="color:#408080;font-style:italic"></span><span style="color:#19177c">$deco</span> <span style="color:#666">=</span> base64_decode (<span style="color:#19177c">$enco</span>); 
</span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">echo</span> <span style="color:#19177c">$deco</span>; 
</span></span><span style="display:flex;"><span><span style="color:#bc7a00">?></span><span >
</span></span></span></code></code>

Output:

<code><code class="language-text hljs" data-lang="text"><span style="display:flex;"><span>HELLO--こんにちは--你好
</span></span></code></code>

The above is the detailed content of PHP Base64_decode function. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:lsjlt.com. If there is any infringement, please contact admin@php.cn delete