Always Use error_reporting(E_ALL)

PHP

I’m mostly a Java guy, who very rarely use PHP. Recently, I just wrote some web services (which is obviously ini PHP) for the project I’m working on. One of them didn’t work as expected, and I could not figure it out (must have been sleepy!!)

function XOR_encrypt($message, $key){
$ml = strlen($message);
$kl = strlen($key);
$newmsg = "";

for ($i = 0; $i < $ml; $i++){
$newmsg = $newmsg . ($msg[$i] ^ $key[$i % $kl]);
}

return base64_encode($newmsg);
}

I spent 2 hours looking at the code scratching my head, wondering why on earth it gave the wrong result. Of course, aware readers will notice the error instantly: the msg variable hasn’t been declared before. And PHP happily ran the code without giving warnings.

Finally, a friend suggested to use error_reporting. I did, and reloaded the code. Then I saw a bunch of errors.

Well, what a waste of 2 hours:mrgreen:

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s