-2

I have been using json_encode, url_encode and many other functions suffixed with encode in php. However, I have never seen real use case or atleast learn from anywhere what base64_encode does. Inspecting its output,I realize that it uses Character set [A_Za-z0-9] to encode data.

//sample output

TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlz
IHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2Yg
dGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGlu
dWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRo
ZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4= 

So the question is. What does base64_encode aim to solve when we already have a myriad of text encoding functions.

SantiBailors
  • 1,397
  • 2
  • 18
  • 38
  • 1
    I've seen it used in payment gateway APIs (BarclayCard SmartPay IIRC) to make a sort of message digest of the transaction (combined with a secret key) which you then POST to them using cURL. – CD001 Sep 05 '16 at 13:51

1 Answers1

2

The documentation on base64_encode() gives you the answer:

This encoding is designed to make binary data survive transport through transport layers that are not 8-bit clean, such as mail bodies.

json_encode converts values into JSON notation and urlencode is used when you need to put values inside an URL. Only because they all have the word encode in their names doesn't mean that they are related in any way.

simon
  • 2,418
  • 1
  • 13
  • 22