I've been coding in PHP at work for the last 5 years. My org's entire backend is written in PHP—a decision made in 2007 when the company first started. It's not a language I ever imagined myself using prior to working there, but life takes you in all sorts of directions you don't expect.
PHP gets a bad rep in the industry, despite being a mature and commonly used language. But it's mostly based on out-of-date understanding of what PHP can do. Recent versions have caught up with most other languages in terms of features; by this point it's a pretty versatile general-purpose language. Certainly not just for serving HTML, as it was originally designed.
I'm no longer working at the aforementioned company, so I'm reflecting on my experience with PHP after all these years and there's some things I've always found odd about it.
And more than just odd, some of it's language features are really unintuitive and have been prone to cause bugs. This comes from personal experience and many previous headaches at work. I'll explain two of the biggest offenders in this post—in short:
Arrays are weird and overloaded The type system is clunky
Arrays Are Not Really Arrays
PHP's standard library basically only has one data structure: the array . This was intentional; it was designed to be a general-purpose, flexible data structure that can cover a variety of use cases. It's technically an ordered key-value dictionary, not an array in the traditional sense.
Unfortunately, with flexibility comes complexity. If you want to create a collection of fixed-size objects in an allocated memory block, you can't really do that. PHP pretends to support them, but the illusion breaks down in unexpected ways.
Let's say I have a bunch of fruits. PHP let's me define a fruits "array" and I can do normal array things with it.
$fruits = ["apples", "oranges", "limes"]; // you can count em' count($fruits) // 3 // you can access em' $fruits[0] // "apples" // you can print em' print_r($fruits); /* Array ( [0] => "apples" [1] => "oranges" [2] => "limes" ) */
... continue reading