Why PHP sucks

The language itself is fairly sane (imho), however the relatively low barrier to entry allows programmers with relatively little experience or PHP experience to be productive. This productivity does not necessarily generate good code. The other day I ran across a bit of code that looked something like:

$var = $_REQUEST['someTooLongFreakingVariableName']?$_REQUEST['someTooLongFreakingVariableName']:date('Y-m-d', mktime (0,0,0,date('m'),1,date('Y')));

There are a bunch of things that I think are wrong with this line.

I try to keep code less than 80 characters, at times my code goes longer. I have known smart people that write lines of code that go much longer than 80 characters, but most of the space is consumed with whitespace from code that is in my opinion too nested. The line of code I encountered was ~200 characters long, mostly *not* from whitespace.

I like the ternary operator, but  there are some places that it beat the tar out of readability. This is one of those cases. In my opinion nested functions and the ternary should not be used together.  Nested functions just makes too much happening on a single line to understand quickly. Even breaking the code up into multiple lines helps as a start.

if( $_REQUEST['someTooLongFreakingVariableName']){
        $var = $_REQUEST['someTooLongFreakingVariableName'];
}else {
        $var = date('Y-m-d', mktime (0,0,0,date('m'),1,date('Y')));

}

Or better yet:

if( ! $var = $_REQUEST['someTooLongFreakingVariableName'] )
          $var = date('Y-m-d', mktime (0,0,0,date('m'),1,date('Y')));

What really annoyed me about this code was the horrible use of the date and mktime function.  The programmer that wrote this knew how date acted without a second argument because of his use of  code like “date(’m')”. The following bit of code I think is a ton more readable:

if(! $var = $_REQUEST['someTooLongFreakingVariableName'] )
        $var = date('Y-m-01');

Comments are closed.