
PHP: Get serious?
PHP is quirky. It’s that odd cousin that no one really admits to being related to (Sorry cousin Rupert). It’s also very simple. It’s interpreted, not compiled, it’s got non-serious function names like implode
and as a result, some think it’s not serious.
People don’t REALLY think PHP isn’t a serious language, after all it runs a LOT of the internet, but coming from C#, PHP has a certain quaintness about it. PHP feels like it’s not serious, like it’s too lax in its syntax. PHP documentation is actually almost fun to read. Idiomatic terms like needle and haystack are used when talking about search inside strings and arrays.
PHP is both approachable and inconsistent, but with enough time and looking for examples ( too many, actually ) the language eventually makes sense… sort of.
PHP documentation is almost fun to read! Terms like needle and haystack are used when talking about search strings inside strings and arrays.
One of the things that is annoying about PHP is that there is often too many ways to do something. While this freedom may seem less stifling, it actually ends up making PHP and easy to screw up. You’re never quite sure if the way you’re doing something is the most efficient, readable, or modern way, as there’s a lot of legacy cruft for backwards compatibility left in the language with few things deprecated.
If PHP is English, C# is Japanese
When I was learning Japanese I loved the fact that every single verb conjugated into the past tense in exactly the same way. Essentially, you chop off the end and add ~ました or ~た and you’re done. It was orderly, it followed the rules and there were few exceptions. Using the language in conversation was surprisingly easy.
Compare Japanese to English which is a language of exceptions when conjugating verbs. If you want the the past tense of a verb, you might add -ed or you might use a completely different form, like went , ate lived, sought, came, could, withdrew, etc. You pretty much just have to memorize the conjugations.
PHP is like English. C# is like Japanese. C# is consistent in the keywords it uses, the parameter names that it uses, and the order in which they’re placed. PHP is not. Sometimes you’ll use a function like in_array
and many times you’ll use array_diff
or implode
or asort or keysort
when dealing with arrays. Functions that have cute names like strstr
or chunk_split
or uc_words
with strings. There is a consistency that a language like C# would have. It feels like a language that is trying to be approachable by adding functions whenever it was needed.
Echo, echo, echo
it’s It’s kind of cool to use echo. After all, echoes are FUN. Who doesn’t like to yell stuff out while hiking into a canyon to hear the canyon walls recursively call it back to you?
The inconsistency in English is a lot like PHP. PHP function names and keywords are kind of a mixed bag. In one hand, you get to use functions that have cute names like
echo
and implode
and chop
and chunk_split
which you have to commit to memory. (I mean… echo
outputs my data to the screen? Shouldn’t that be a recursive function or something?) Instead of echo
you expect some word like output
or write
or print
would be used. But, nope… someone chose echo
and it IS kind of cool to use echo
. After all, echoes are FUN. Who doesn’t like to yell stuff out while hiking into a canyon to hear the canyon walls recursively call it back to you? Wait… maybe it is a good function name for recursion.
Structure and sense, rules and regs
C#, like Japanese, is more structured. Instead of echo
we have Response.Write
. Instead of implode
and chop
we have the clearly named String.Join
and String.Trim
. In addition, you can just attach these methods right to your variables. If you have a string called emailAddr
you can just write emailAddr.Trim()
which performs the action on the string. You suffix the action (the verb) to the end of the variable (the noun) just like Japanese consistently does.
The method names in C# clearly define their purpose. They belong to the string class. They modify strings. The syntax in C# is consistent, easy to remember, and easy to discover. The syntax follows a specific set of rules. There’s a lot to like.
But with rules comes restrictions. You can’t for example, just pass anything anywhere. If a method wants a String
object as a parameter, you can’t just send it an array, or a character, or a number. You can’t even really send it a Char
object. If a parameter in a PHP function wants a number, you can send it anything and it will just try to convert it in many cases.
In C#, types are checked and they are enforced. While this restriction may seem limiting, in practice having the freedom to have a variable be anything at anytime in PHP can cause more hard to track bugs if you’re lazy when programming. It took some getting used to this in PHP and you end up spending more time in the documentation, for better or for worse. It does force you to keeping your code style consistent, but it can be frustrating when tracing bugs.
It IS easier to get PHP up and running and it’s definitely a simpler, more forgiving language and framework that’s easier to understand. And perhaps that’s why, like English, it’s been adopted by so many. For all its warts, it’s easy to learn. It’s only when you get to larger projects where structure is important. Something that C# enforces from the get-go. I still think it’s weird using .
to concatenate strings together.
So, that just scratches the surface of the syntax challenges that I had when going from C# to PHP. There’s a whole lot more, but this article is long enough. In a future article… I’ll talk about View Engines. Razor, ASP.Net Webforms, PHP, etc.