php base64_encode() – How to Use Base64 Encoding in PHP?

5/5 - (2 votes)

php base64_encode Examples : The base64_encode() function in PHP encodes data with MIME base64. base64 encoded data consumes 33% more space. Means encoded data is 33% larger than original data.

php base64_encode

There are the following the list of “php base64 encode”.

php base64_encode – Supported Versions:

  • PHP 4
  • PHP 5
  • PHP 7

PHP base64_encode() Syntax

base64_encode ( string $string )

string $string | required : string value to be encoded.
Return Value : base64 encoded string return करता है।

Read Also:  How to Increment and Decrement Column Value in Laravel 6?

PHP base64_encode() Example

<?PHP
  	$str = "Welcome To Pakainfo.com !";
	echo base64_encode($str);
?>

//Result
V2VsY29tZSBUbyBQYWthaW5mby5jb20gIQ==

The base64_decode() function is used to decode a base64 encoded string.

How to convert an image to Base64 encoding?

$path = 'rajkot/loveshop.png';
$type = pathinfo($path, PATHINFO_EXTENSION);
$data = file_get_contents($path);
$base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);

base64_encode

base64_encode ( string $string ) : string

base64_decode

base64_decode ( string $string , bool $strict = false ) : string|false

@ref : https://www.php.net/manual/en/function.base64-encode.php

store the result in a variable and check for errors

Free Live Chat for Any Issue
Download

<?php
$str = "Welcome To Pakainfo.com !";
$b64 = base64_encode($str);

if ($b64 === false) {
  echo 'Invalid input';
} else {
  echo $b64; //-> "V2VsY29tZSBUbyBQYWthaW5mby5jb20gIQ=="
}