One of the reason people hate php

Php is by far the most popular web development language. There is a lot of effort put by the programing community to promote Python, ruby on rails, and some newer technologies, but php (LAMP) is just so easy to install and get started. I am a regular php user and it works just fine for my needs. The thing is sometimes I stumble upon some code that make me understand why people hate the language.

I was going through some old code on a website I work on and there was an interesting bug fix i found.

One developer was writing a search query for a page and used 6 variables since there were 6 values in the database:

$p_name1 = ($dbobj->getPopularname1() == '') ? $official_name : $dbobj->getPopularname1();
$p_name2 = ($dbobj->getPopularname2() == '') ? $official_name : $dbobj->getPopularname2();
$p_name3 = ($dbobj->getPopularname3() == '') ? $official_name : $dbobj->getPopularname3();
$p_name4 = ($dbobj->getPopularname4() == '') ? $official_name : $dbobj->getPopularname4();
$p_name5 = ($dbobj->getPopularname5() == '') ? $official_name : $dbobj->getPopularname5();
$p_name6 = ($dbobj->getAdditionalAlphaName() == '') ? $official_name : $dbobj->getAdditionalAlphaName();

Now way down in the code, another developer saved in an array of size 6. Now a little lower another developer made use of the the 7th element of the array. As you can see this will cause a warning:

Notice: Undefined offset: 7

You will assume that the fix will be not to use index that is not defined but here is how it was fixed:

for($i=7; $i < 45; $i++){
   if(!isset($array[$i])) $array[$i] = ${"p_name".rand(1,6)};

Yes they randomly created 45 elements in the array. In case someone was to use index 12, it will be present.

I didn't know what to say when I saw this. I am sure this is why people hate php, because someone can get away with it. This is not the fault of php though, it is just bad programming.

Just had to share it.


comments powered by Disqus