Implementation of Base64 in PHP - Test

This section provides a test program for the default PHP implementation of the Base64 encoding algorithm - base64_encode() and base64_decode() functions.



The Base64 encoding algorithm bas been implemented in PHP language as 2 built-in functions since PHP 4:

To test these built-in functions, I wrote this simple testing program:

<?php # PHP_Base64_Test.php
# Copyright (c) 2007 by Dr. Herong Yang, http://www.herongyang.com/
#

echo "\nTest 1:\n";
test("A", "QQ==");

echo "\nTest 2:\n";
test("AB", "QUI=");

echo "\nTest 3:\n";
test("ABC", "QUJD");

function test($theInput, $theExpected) {
   $theEncoded = base64_encode($theInput);
   $theDecoded = base64_decode($theEncoded);
   echo "   Input   : $theInput\n";
   echo "   Encoded : $theEncoded\n";
   echo "   Expected: $theExpected\n";
   echo "   Decoded : $theDecoded\n";
}
?>

Here is the test result:

C:\herong\base64>php PHP_Base64_Test.php

Test 1:
   Input   : A
   Encoded : QQ==
   Expected: QQ==
   Decoded : A

Test 2:
   Input   : AB
   Encoded : QUI=
   Expected: QUI=
   Decoded : AB

Test 3:
   Input   : ABC
   Encoded : QUJD
   Expected: QUJD
   Decoded : ABC

The result matches my expectation perfectly.



 

Table of Contents

 About This Book

 UUEncode Encoding

Base64 Encoding

 Base64 Encoding Algorithm

 W3C Implementation of Base64 in Java

 Sun Implementation of Base64 in Java

 Sun Implementation of Base64 in Java - Test

 Goetz' Implementation of Base64 in JavaScript

 Goetz' Implementation of Base64 in JavaScript - Test

Implementation of Base64 in PHP - Test

 Base32 Encoding

 URL Encoding, URI Encoding, or Percent Encoding

 References

 Full Version in PDF/EPUB