The craziness that is PHP

For my non-programmer readers, this is probably a new and foreign topic to you, but for my mid-level to experienced web developer readers, this topic is definitely nothing new. In fact, you have probably read this article before and sent it to fellow programmers (that is how I came across it). The flaws, issues, and downright craziness of PHP is so elaborate and elaborately documented, hated, raged about, ironically depicted, debated over, cried over, apologized for, and just about any other describing phrase you can add here, that the language has almost become- if not already is- the laughing-stock of server-side programming.

In that regard, this blog post is nothing special. Though I have used PHP for a months (I only learned enough to get by in database connections class) and have “seen nothing yet”, two years of programming experience, two other programming languages, HTML/CSS, some SQL, and many programming projects have allowed me to see and learn quite a lot about programming and know more than enough to tell when a language is really bad. PHP, despite its popularity and widespread use, I already know it is a very, very bad language.

In the following code snippets, I would like give a taste of some bad PHP behavior, why it is bad, and how I would expect it to act by comparing it to JavaScript and/or Python, which are much more *ahem* sane languages. Mind you, these demos are not earth-shattering complex either. They are everyday, run-of-the-mill, boring cases, but they are cases fundamental to any programming language. PHP’s behavior in these areas is so backwards that it confuses many newcomer and experienced programmers alike because they are so wrong when stacked against virtually every other programming language ever made.

Let’s get started, shall we?

Quoted array keys

echo $person['me'];
echo $person[me];

Ooooooohhh boy, this is a rather interesting case to start on. Here, we are accessing the key 'me' in an assumed previously defined associative array (JavaScript objects, Python dictionaries) known as $person. In both JS and Python, accessing the key requires surrounding the key name in quotes (that is, it must be a string) within the square brackets. The only time this is not required is when a variable is used for the key, and even then, the variable’s value must be a string. As we can see in the first statement, PHP supports this universal method of access.

Now look at the second statement. Here again we are accessing 'me' in the $person associative array, but notice the lack of quotation marks. PHP will very happily treat this as a valid key and retrieve the value for you. This is not good behavior, as it can create confusion with user-defined constants. How do you know you are using a named key or using a defined constant?

Accessing arrays

echo $myArray[0];
$myArray[0] = 'Item 1';
echo $myArray[0];  // Outputs: 'Item 1'

Continuing on the topic of arrays, the first statement in this snippet is attempting to access index 0 of $myArray without creating it first. In both JavaScript and Python, attempting to access an undefined array will throw an error, the universally expected behavior. Instead, PHP returns NULL. The second statement attempts to add the string “Item 1” to index zero in the same undefined $myArray. Again, both JS and Python throw an error here. Instead, PHP implicitly creates the array and assigns the value, granting success to any subsequent access (the final statement).

I am trying to think of a time this would be expected and/or good behavior, but I keep drawing a blank. Further, the PHP manual discourages using such a “feature”, but programmers are lazy and those starting out do not know any better. Do you really think the warning will be heeded?

While we are at it, we are going to look at the documentation for array_push(), a convenience function that also appends values to an array- but with a twist: it checks if the array passed exists first.

Note: array_push() will raise a warning if the first argument is not an array. This differs from the $var[] behaviour where a new array is created.

It is sorely disappointing that it takes a function to check if the given array exists before appending while the native syntax has no such safeguard.

Mixed array types

// Create an indexed array
$family = array('Me', 'Myself');
$family[] = 'I';
print_r($family);
// Outputs: Array ( [0] => Me [1] => Myself [2] => I )

// Now create an associative array
$me = array('name' => 'Triangle717', 'age' => 20);

// But add an indexed key to it. This should error, right?
$me[] = 'Programmer';
print_r($me);
// Outputs: Array ( [name] => Triangle717 [age] => 20
// [0] => Programmer )

I would like to know in what other programming language can you have both indexed and associative keys in a single array-like data type. From what I know, that number is zero.

Functions that imply multiple uses

We could go on for a long time, but we will end our tour on this final point.

PHP suffers from a poorly named and poorly designed myriad of functions, but this one hit me while working a project for PHP class. Maybe it is just me, but I would expect a function named empty() to check if the value given, be it a string ("" or all whitespace) or array (no values), is empty. What does it do instead?

Determine whether a variable is considered to be empty. A variable is considered empty if it does not exist or if its value equals FALSE.

That is right, it checks if a variable is undefined or has a falsey value. That is the last thing I would expect it to do. Oh, but this was changed in PHP 5.5 to support more than variables, so empty("") === true works now!

Yea, that is if you are lucky enough to be running an updated version of PHP. College issued me a subdomain on a Bluehost server, and it runs PHP 5.4.23, built on 23 December, 2013. No, none of that is mistyped. Thus I did not have access to the latest and greatest PHP improvements, forcing me to rely on preg_match('/^\s$/',$myString). I might not have been confused if the function was implemented correctly the first time or even given a more descriptive name.

OK, that last bit got a bit ranty. Sorry about that. 😛

-le717

Advertisements

2 thoughts on “The craziness that is PHP

Triangular Reactions

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