All the Reasons why I hate PHP

0. No out-of-the-box framework for MVC or other frameworks for organizing your homepage

If you want to use a MVC framework or a template system, you either have to rely on a 3rd party implementation or write your own. No such thing in the PHP standard library.

1. PHP is a weak-type language

In PHP we do not care about types. If you used this variable for Foo, but you want to store an integer in it? Sure, why not? In PHP, we define the type of our variable/return-value/parameter in a comment. If you accidentally use the wrong value in a 200 lines long algorithm, PHP wouldn't even bother you that you are using the parameter value instead of a value inside a table. 

2. Lack of proper and clear documentation

Although every function in PHP is documented, you have to think twice in some cases. A good example is the str_replace function. The description for str_replace says the function is declares as following:
mixed str_replace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] )
You would think that the $search value is the string you want to perform the string replace function on until you see the subject parameter. But wait, maybe subject is the value you want to search for, or wait - is that $replace?
In C#, it is solved as string Replace(string oldValue, string newValue). 
A better declaration would perhaps be:
mixed str_replace ( mixed $oldValue , mixed $newValue , mixed $subject [, int &$count ] ) 

3. Lack of design for secure code

This is a side-effect of that it is a weak-type language. Instead of defining that this parameter is an integer (well, those of us that actually write comments have this here but comments does - surprisingly not have any affect on our code execution) you have to manually check that it is an integer. PHP almost got this fixed by adding type-hinting where you can define the types of the parameters to your functions. But there is a catch (as with everything else in PHP) - you can only use type-hinting if there are objects. Arrays, integers, strings, etc. are not supported for this feature. This way you have to add if checks for every single variable then perhaps throw an InvaldiArgumentException if you integer is a string instead. 

4. No overloading of functions

For whatever reason, PHP does not support this feature. If you have a "function getUser($userID)", you cannot write "function getUser($userID, $password)". Amazingly enough, PHP does have default parameter values though. 

5. No consistent style of API functions

One example is the DateTime object and other PHP library functions. Most functions in the PHP library are like some_function(). Then suddenly DateTime has a function named someFunction() instead. Although I prefer the last notation, my code gets inconsistent when I use different programming styles to call simple library functions. 

6. Every variable is declared with a $

I have to find the Alt Gr button then hit the 4 key to get a dollar sign. Of course, var is another key extra, but it is a lot easier to type than a $ sign.

7. Where are my enums?

PHP does not support enums. Instead we define our values as consts under a class. Want to make sure your value is a valid "enum"? Then you must write a function for that.

8. Lack of good habits in example code and forum threads

PHP is an incredibly simple language. Someone can easily learn PHP if they have any background in another programming language, and people with no programming background can also easily read and understand PHP code. There is one problem with this: The easier a language is, the lower quality will there be on code floating around the net. This makes it harder to become really good at writing good PHP code as most of the example codes around the web contains at some point bad habits which prevents you from learning how to write good PHP code. 

9. What's up with the underscores?

I have to write __ when I override system functions like toString and constructor. Why can't I just write a function Foo() as the constructor declaration for my class Foo?

 

PHP is not a bad language, neither a good one. It seems that PHP is getting old and more and more hairy after time. If I had the choice, I would write my homepage in C# or Java if this was an option. Although there is a lot of hate around PHP, PHP is still good in some cases. 

Web-Programmers and Programmers

I am in the middle of a school project where we are making a dynamic webpage using HTML, CSS, PHP and a dash of Javascript. Some of the languages has caused hours of frustration and anxiety as things does not behave logical. It is easy to see that these are from two clearly different worlds.

Although PHP is in fact the language I learned before C#, it is not a language I like. Unlike C#, it is a weak-type language which means it does not really care about the type of your objects. Conversion is automatically made and you don't need to worry if one variable is used as two different types. Imagine if you had a box where you could store values in. In PHP, the language doesn't care what box it is as long as there is a box. In strong-type languages like C++, the language requires a special box for integers, one for character and another one for decimal numbers. PHP is not the only weak-type language. Javascript is also a weak-type language and C# is slowly coming after. So why is this such a big deal? Wouldn't types be one thing less to worry about?

Documentation is something we are encouraged to make already from the first class of programming. But the fact is that we are really lazy when it comes to commenting our code. It is what we do after we code and we often tend to skip it. In PHP it is a common practice to comment the return type of a function in the comments above the function by using the @return directive. C-based languages always declare the return type. public static int getInteger(); In PHP it would be public static function getInteger(). If someone from a strong-typed language begins to read PHP code (if there are anyone of those), things can get quite confusing. "Wait, it is supposed to return something, but it clearly says it is a function!" The programmer thinks function = void as this is always used in Java. 

For whatever reason, PHP has decided to allow programmers to declare the type of the input parameters to a function with one exception: Data types such as integer, strings, floats, etc. are not allowed. This caused frustration when I thought my PHP code actually was wrong a while ago when I got the error "Argument 1 passed to function() must be an instance of integer, integer given". And for whatever reason, type-hinting is not supported on functions with a return type. 

CSS is fun. You wanna do something as simple as vertically center something? Then you probably have to add more HTML and a dozen of CSS properties just to get your object centered. And let's not get into which browser you wanna support or which version of those browser(s). CSS is literary hell. C++ is a standardized language. So is C. This means if you write code in standardized C++ - say C++11, it would be interpreted exactly the same way by every single compiler which supports C++11. Now here comes the fun part: CSS is not even a standard. Your CSS can be interpreted differently on Chrome, Opera and IE. W3C only comes with recommendations for CSS. Let's say you want to write a browser and you want to implement the CSS3 recommendations which W3C has made. Then you find out that Microsoft, Google, Mozilla and Opera has implemented it differently - all of them. After my experience with XPath and Javascript, Chrome didn't bother when I modified an object from an XPath result. Firefox threw an exception when I made changes to an object and IE didn't even support the document.evaluate function.

Web-languages are from a different world. They are easy and they most likely doesn't work the way you expect them to work. There are no standards, and everyone wants to implement a feature different than anyone else because their way is the best and only way to do it. Sure, it is easy to write PHP code. But the amount of documentation required is higher than C#. Self-documenting code is a key-word. It makes me glad to think about that I didn't chose to become a web-developer as I honestly do not understand how web-developers are able to sleep during the night.

About the author

I am a software development student while I work with C# and C++ applications in my free time. I also play a lot of Xbox games (Halo ftw!) and I am a huge tech enthusiast. I've been coding in C# for about 5 years now. Always been interested in code-optimizing, databases and networking. And if you got a cookie, you better give it to me.

Month List