I am so tired of people defending PHP.
Note: this is good natured. The guy that wrote the original article — why can I never find names on these things? — makes a few good points. And, for full disclosure, I work with PHP full-time right now, and it’s still my go-to language for knocking out a web application because I’m so familiar with it. (And even then, only with Zend Framework and phpSprockets.)
But seriously, PHP is sickening. I’ve started using Ruby and Python for any tasks I can get away with and they’re really worlds apart. PHP’s got some good in it but it’s all buried under deep layers of hate. That’s right, PHP hates you.
PHP is terrible
Need proof? Here it is, broken down by category:
Language Features
- So many things evaluate to boolean false or boolean true that there had to be a new operator introduced (=== and !==) to do strict comparisons.
- You can’t differentiate between a hash or a list or anything. They’re all the same. Even if you know you’ll never need a keyed index, you can’t turn it off.
- It’s Object Orientation is broken in places (late static binding).
- It doesn’t support lambas. create_function() is not a lambda.
- You never know if short_open_tags is on, or if safe_mode is on, or if (ugh) register_globals is on.
Built-in Functionalty
- It comes with a standard library that has no naming convention. You never know in what order the words will come in or if they’re separated by underscores or not:
- str_replace()
- strlen()
- parse_str()
- You never know what order the arguments are in:
- in_array($needle, $haystack)
- strpos($haystack, $needle)
- You constantly have to check your output thanks to type restrictions:
- strpos(’abcd’, ‘a’) will return 0. Don’t test this with if (strpos(’abcd’, ‘a’)) { … } because 0 is secretly false! You’ve got to use if (strpos(’abcd’, ‘a’) !== false).
Common use
A lot of open source projects are written in it, such as Joomla, Wordpress, and MediaWiki. All of their code is hideous, and half of the time there’s random HTML in the middle of their scripts because, of course, we’re working with webpages and why would you ever want to abstract the output from the logic?
Irrelevant praise
Here are some common praises of PHP and why they don’t matter:
PHP is great because it allows the new user to pick it up quickly!
Sure. And if the introductory material available covered good programming style we might not have a problem. In reality, every example PHP script is riddled with SQL Injection vulnerabilities, XSS vulnerabilities, and terrible programming. Most newbs never learn a better way of doing things because this gets them results that look good very quickly.
PHP has classes, it doesn’t need namespaces.
Fail. Use a language with namespaces for a few months and come back to PHP. I did. It’s sickening.
Globals are okay!
Whoever taught you that needs to be shot. Globals are not okay. They are tolerable in certain situations, but if you make blanket statements like “Globals pierce through all the layers of code and get right to the heart of the matter.” then you’re clearly using them too much.