I often hear people slagging PHP off because of the way some people use it.
Would anyone on here be able to provide a 'good php' and 'bad php' example just for reference like
I'm not a PHP programmer (at heart, or by profession), so I can't point to particular examples of good and bad PHP, but I
can tell you that it doesn't matter: good PHP is the same as good Python or good Ruby or good Perl.
There are always two levels of badness: design (i.e. architecture, structure, separation) and implementation (the tools/software, language and actual code). Every program has an overall structure and concept (hopefully) and an actual implementation. PHP encourages bad structure, and hinders good implementation.
Architecturally, a good PHP application will look like a good Python, Ruby or Java application. It doesn't matter: all good apps are based on some solid model/pattern like
MVC,
MVP, the
actor model or whatever. At any rate, they have a robust, clean, logical, and extensible core concept.
It's similar to writing a CSS file: you try to find commonality between elements and abstract and combine that commonality.
That is the foundation of good development: finding the right model/structure for your application; and that is the principle PHP, by its fundamental nature, rapes. PHP is essentially a programming language embedded in HTML, which is arse-about-tit right off the bat. HTML is
always subordinate to the code. It's a
markup language, FFS. So PHP enables, nay encourages, you to get into some filthy habits from the word go. Including 'business logic' (i.e. application behaviour, not UI behaviour) in the UI code; directly calling SQL on the database; not validating user input; not organising your site from the start; copy-pasting-code; etc. In short, PHP makes things that are very difficult appear easy by
hiding the problems, not solving them. Any Python, Ruby or especially Java programmer faced with the task of interfacing with the Web will immediately see the enormity of the situation; the PHP programmer is given no hint.
Of course, that won't discourage experienced developers, but the PHP
language itself is shitty. Originally, it was a thrown-together bunch of scripts, and that still shows today. It was originally modelled after C (no namespaces) and suffers from badly- and inconsistently-named functions, and higher-level features feel tacked-on. Technologically, it's also shit: PHP is far slower than other interpreted languages.
So, at the implementation level it sucks beyond help compared to other languages. Still, it's worth learning, because it's ubiquitous.
Basically, PHP is the C of the Internet. It isn't, wasn't and never will be the best language, but it's just good enough that it will run anywhere, and any twat can use it. And so it is everywhere, and every twat is using it.