Archive for April, 2014

a horror of php

Uncategorized | Posted by Gauthier
Apr 17 2014

To start off with PHP has always been on my list of acceptable languages to test things out with. Today i decided to add database queries and times to my tiny library to speed things up. The reason was after reading this article .After implementing a quick tally, My code all worked flawlessly, now it just needs something to display on the GUI.

Someone I know using my code suddenly had all of his code break. At first I couldn’t even guess what the problem was, but it boiled down to uninitialized PHP variables.

They write code like the top half. I change my function to be like the bottom half.



function x() { return 'z';}
function data() { throw Exeception(); }

$data[itemone]['array'][1.+2][x().y][$news][] = data;

// GUESS WHAT!
echo '['.print_r($data, true) . ']';

// vs this little bit of logical code

if (array_key_exists('somedata', $otherdata)) {
$otherdata['somedata'] = array();
}

echo '['.print_r($otherdata, true) . ']';


The concept that PHP magically makes variables and nested arrays breaks my head a title.

I alerted them of the uninitialized variables, also I altered my function to check if the parameter in the function is an array.



if (!is_array($otherdata)) {
$otherdata = array();
}
if (array_key_exists('somedata', $otherdata)) {
$otherdata['somedata'] = array('timer'=>0);
}

echo '['.print_r($otherdata, true) . ']';