Why is PHP so ugly?

In short...

PHP is old and has gone through many versions.

In long...

PHP's syntax is influenced by Unix bash shell style scripting, C langauge, object oriented programming and dynamic language constructs (similar to what you would find in a language like JavaScript). It supports some types and dynamic typing. In its early days, naming conventions were not well established, hence why you have the function isset while all the functions like is_array, is_finite, etc have underscores. One open source PHP extension library will standardize to camel case (isArray) while another to underscores (is_array). Many functions have also been deprecated, but continue to exist in the language. This is because Zend technologies maintains the interpreter and makes an effort to keep it backwards compatible whenever possible, unless it conflicts with performance or moving the language forward to an enterprise level language. It has gone through many iterations, the most messy of which was going from a procedural language to an object oriented language from 4.0 to 5.0. Since it will run as both, this leads to some messy hacks from developers at times. PHP was also among the first languages created to build web applications, because of this it has lots of legacy ideas remaining in the code from the early days when they were figuring stuff out. It has been around for a long time so code bases are rife with legacy code and applications are often an ensemble of cut and past programming from a collection of code snippets found across the web. Besides the history of the language, the main reason it can be ugly is because it is so flexible. This is simultaneously why it is such a popular and powerful language while also being a language prone to ugly code and loathed by many (ruby and java developers especially). Despite a lot of developers thinking PHP is ugly, modern PHP has come along way and can be well organized and pretty when done right.

Comparing PHP to other popular languages

A note to developers of other languages getting into PHP. Not everything that makes a language ugly has to do with the language its self. The further the language is from what is familiar, the more ugly it becomes. So why not make languages more like English script, since we all can read? Remember we tried that with SQL, except in the case of simple queries SQL is a readable nightmare. There is a balance between the readability of a language, the speed at which you can code (short hand), the amount of features a language contains and, if it's interpreted, the speed at which it can be parsed. PHP tries to balance these, for instance the annoying $ that is in front of every variable makes the interpreter able to parse the code faster, but is annoying to type over and over. If you are not familiar with shell scripting and C, PHP will look very ugly indeed, but have patience as you get use to it. Once you get use to it then it's more the aspects of the language that I described above that make the language unpalatable at times.

PHP vs. Java

To me PHP is less readable than Java because PHP is less uniform and also I believe, because there is a much more copious use of non alpha numeric characters. That is to say, it is further away from everyday English script than Java. For example, the use of a dollar sign at the beginning of every variable like $somevariable, clutters the screen, yet it allows for nice features like injecting variables into double quoted strings and makes the interpreter faster (as mentioned above).
Ex:
echo "this is the result of some variable $somevariable which is a string";

Where as in Java it would be:
println('this is the result of some variable '+somevariable+' which is a string');

The use of these devices increase the speed at which you can code. Clearly, it is one of the reasons you can write code in PHP faster than in Java, except maybe when auto complete is running in Eclipse. So it is always a trade off. Perl was an example of a language that could be coded very quickly, but proved difficult to read other peoples code. As you become more familiar with PHP, it of course becomes more readable, but to a point. Even when you are experienced the various special characters employed _POST, __construct, etc. begin to take their toll. In the end I would say that PHP is ugly, but some languages have to strike a balance between short hand and readability and PHP is some where between Perl and Java. If your company is functioning, bringing in revenue and the most important thing is stability then Java can be a better choice on the basis of its maintainability in comparison with PHP and compile time checking. Though PHP is always making strides to use frameworks to make it more maintainable. If you need to iterate quickly then PHP can be great. In the end, I can't say truly what is good, bad and ugly when it comes to popular languages, it's all about trade offs.

Remember PHP is the most programmed language on the planet at the time of this writing.

comments powered by Disqus