

What I Like About PHP - simon_w
http://coding.simon.geek.nz/2014/12/10/what-i-like-about-php/
A couple of the things I like about PHP, to counteract all the negativity it gets.
======
xyby
I'm a programming language geek, a code lover and a syntax fanatic. And there
are a ton of things I love about php:

    
    
        class Animal // yeah! real classes!
        {
          function hello($name="nobody") // nice default variables
          {
           echo "hi $name"; // sexy variable expansion!
          }
        }
    
        class Dog extends Animal // logical syntax :)
        {
         ...
        }
    
        function f()
        {
         $x=123; // variables are local by default and need no initialisation
        }
    

I could go on for a long time, listing all the language constructs I like in
PHP. All other languages I tried have quirks that annoy me.

~~~
chc
Remarking on the fact that variables are local by default makes me wonder
exactly how many programming languages you're familiar with. That's an
extremely low bar, not really something to love.

~~~
xyby
Low bar or not, if you do anything web-facing, there is no way around
javascript. And you will do a lot of stuff in a language that makes variables
global by default.

~~~
Shish2k
A car metaphor for this conversation:

"I love my skoda! It has four wheels, and a roof, and a passenger seat!"

"That's a pretty low bar for things to love about a car. Pretty much all other
cars also have those features, and they do them better."

"Yeah, well I also ride a bicycle, and my bicycle doesn't have those
features."

------
adamnemecek
> It appears that just because PHP has been used a lot more than most
> languages it gets compared to, its warts are known about a lot more which
> makes it a much easier target.

Yeah, that's not the reason why people complain about it.

~~~
veb
Then why do people complain about it in your opinion? Shouldn't leave the
conversation hanging ;-)

~~~
Mithaldu
There's no need to continue a conversation that's been had a thousand times
already.

~~~
ChikkaChiChi
I haven't seen the obligatory fractal link yet!

~~~
veb
[http://eev.ee/blog/2012/04/09/php-a-fractal-of-bad-
design/](http://eev.ee/blog/2012/04/09/php-a-fractal-of-bad-design/) (it's
actually a pretty decent article, anti-PHP or not.)

~~~
krapp
I've started using this to measure the quality of a PHP thread. When a thread
reaches 3 Fractals you may as well bail.

~~~
Shish2k
You wait that long? I go into them knowing from the start that the whole
thread will be divided into "PHP is terrible", "PHP is terrible but it gets
the job done", and "PHP is my second language (after javascript) and I think
it's great" camps - no particularly useful discussion will occur from the
first two camps, but we can at least draw some positive value from the thread
by enlightening the third group (or laughing at them, depending on how
stubborn they are).

~~~
krapp
It usually doesn't take very long. It's like a compulsion for some people.

------
nodesocket
The number one reason PHP is liked and adopted so much for the web is there is
no state. Each and every request starts fresh, which reduces a lot of
complexity and the barrier to entry. Also, the fact that it is in itself a
templating engine, makes it faster to produce apps.

~~~
chipotle_coyote
Oddly, it was that realization -- more than all the other many nits people
pick with PHP -- that started making me want to claw my eyes out when I
thought about PHP.

PHP has come a long way in many respects, but it's still fundamentally wedded
to a 1999, CGI-ish model of the web: pages are fundamentally static entities
that you add dynamic bits to as needed. It's a "template engine" because it's
basically designed to embed scripts that we would have previously stuck in
/cgi-bin right in your web pages. In that world, PHP was pretty awesome.

But in a world of MVC frameworks, "every request starts fresh" means that on
every request, the entire framework starts from scratch. It loads libraries
and configuration files and ORMs and controller routing on startup -- and it
has to do it every single time. Yes, I know things can be and are cached,
precompiled, lazily loaded, etc., but that can't completely overcome that.
From what I've gathered over the years, PHP is a pretty fast language -- but
PHP frameworks consistently get their asses kicked in benchmarks, and I can't
help but suspect there's a correlation.

~~~
Iftheshoefits
Rails consistently under performs Django, and both under perform HHVM, go,
Java, and C++ frameworks. Benchmarks don't seem to be as important as the
perceived social status of a language.

------
claar
Most people complaining about PHP haven't actually used it in an enterprise
environment.

Using JetBrain's PhpStorm with a proper framework is simply not the PHP of 10
years ago.

One of my favorite features is type enforcement; consider the following code:

    
    
      class User {
        function is(\Auth\Role $role) {}
      }
    

If something other than an Auth\Role object is passed in, a runtime exception
occurs. Better yet, PhpStorm will check the types in real-time and underline
type errors immediately.

~~~
Dewie
> If something other than an Auth\Role object is passed in, a runtime
> exception occurs.

I think that would be called strong typing (as opposed to type enforcement).

~~~
arenaninja
I think they called it type checking when it was introduced, I would wager to
avoid it being confused with strong typing because in PHP you cannot typecheck
primitives (as of 5.6)

------
fake-name
Wait, wait, the PHP documentation is __GOOD __? Wat?

I mean, it's certainly _extensive_ , but it's rife with the worst programming
practices, and extremely bad/unsafe examples.

~~~
k__
lol, yes.

The best thing are the comments in the docs of people telling you how wrong
the docs are.

~~~
wanderr
No seriously, that is actually one of the best things. The comments on PHP
docs are far more informative than the actual documentation for many other
languages. Most everywhere else the caveats are simply left up to the
developer to discover the hard way.

~~~
k__
They saved my ass plenty of times, but I wouldn't count them to the quality of
the docs.

------
DigitalSea
I personally don't think PHP is that bad of a language either. I mean all
languages have caveats, there is no such thing as a perfect language. I think
PHP gets a bad rep because there is a lot of bad code out there and people
blame the language for the bad code, which is like blaming car manufacturers
for drunk drivers. But some of the criticism is legitimate and we would all be
very naive regardless of our stance to not at least admit that PHP has some
design issues.

The biggest issue I have always personally had with PHP is parameter
consistency. A few might laugh and think this is a pretty small thing compared
to other issues that PHP has, I agree, but it is annoying nonetheless. Some
inbuilt methods want the needle first, others want the haystack first. Array
functions tend to prefer needle first then haystack, string functions tend to
prefer haystack first then needle.

The problem is PHP is a culmination of years upon years worth of backwards
compatibility. The quirks around the order of parameters on inbuilt methods
will probably never be addressed, it is something we just have to deal with.
There are other quirks I won't bother detailing, but I still like the
language. However I do acknowledge that parts of the language are a series of
hacks. While PHP is not a perfect language, it works. And we also have to
acknowledge not too long ago, Javascript was a pretty horrid language with a
tonne of quirks to work around as well and will continue to be until we can
support great ES6 features without polyfills.

I won't pretend to be a code elitist. I use whatever gets the job done. When a
client has a limited budget of $2000 and wants a new website they can manage
themselves, nothing beats Wordpress and Wordpress is built on PHP. No other
language has a CMS that rivals the power of Wordpress (trust me, I have looked
as well). I would argue many people that still use PHP are using it solely
because of Wordpress.

Not to mention PHP just works out-of-the-box with almost every hosting
provider on the planet with minimal configuration (if any) and can easily be
tweaked for great performance (caching and compilation). I still think that
PHP has a future ahead of it. It seems there are efforts underway to make it
better, Facebook have contributed greatly to the language especially. We might
one day see something like ECMAScript 6 happen for PHP where some of the
biggest complaints are addressed (who knows).

~~~
pconner
> I think PHP gets a bad rep because there is a lot of bad code out there and
> people blame the language for the bad code

I think in 5-10 years, in a post "everyone should learn to code" world, people
will say some of the same things about Node.js or Rails that have been said
about PHP for the last decade.

Beginners are going to make mistakes, and PHP was appealing to novice
programmers (for a lot of the reasons listed in the article).

~~~
imanaccount247
>people will say some of the same things about Node.js or Rails that have been
said about PHP for the last decade.

Really? Please tell me which of these things:
[http://webonastick.com/php.html](http://webonastick.com/php.html) applies to
ruby. People have been complaining about javascript being shitty for just as
long as javascript has existed.

~~~
pconner
I don't know enough about Ruby to make any specific criticisms of the
language. I just know that Ruby/Rails is being recommend to beginners (through
Code Academy). I guess I could have said Python instead, but I don't think
Python's web frameworks (Django, Flask, etc) have the market share that Rails
does.

~~~
imanaccount247
So, you have no basis to support your claim, and admit you do not have the
understanding required to make such a claim. Then why did you make it?

~~~
pconner
I never said people would criticize Ruby because of the language itself. I
said that it could POSSIBLY (note that this is pure speculation) get the same
stigma as PHP because it draws in a lot of beginners.

~~~
imanaccount247
And my point was that PHP does not get stigma because it attracts beginners.
It gets valid criticism because it is horribly broken. That's why I showed you
one of the many sites making those criticisms.

------
gfosco
It's still serving 82% of websites, and it runs some of the largest and most
successful properties. It's flexible as all get out, and powerful whether
you're running a site or just some command line scripts. I don't do too much
with it these days (besides the Parse and Facebook PHP SDKs) but I'll always
defend it.

~~~
wyager
>It's still serving 82% of websites

Popularity =/= quality.

>It's flexible as all get out

As opposed to what? Lots of modern languages have extensive web support and
can have libraries for most common tasks.

~~~
gfosco
It doesn't have to be in opposition to anything. There are lots of languages,
use whatever one you like that is acceptable for your situation.

~~~
wyager
>It doesn't have to be in opposition to anything.

If it's not unusually flexible, why extol its flexibility?

------
bdg
I truly don't understand this constant crying about programming languages that
our community participates in. Seriously. Great frameworks and libraries out
there exist and hide most of the cruft you deal with.

I write absurd amount of code and I truly do not start crying because I'm
using PHP, JS, or something else. I run into style issues, for instance, in
PHP I might use is_num instead of typeof foo === "integer" like in javascript.

When I have a problem with a tool I'm using, it's more like "Arg, the API for
new modules in angular is too close to the API for calling already created
modules!!" not, "Oh no, jQuery is bad because Backbone is bad, because
backbone is written in JavaScript, and you can do some mighty awful things in
JavaScript!"

If you're crying about things on the language level, you're not building
robust systems.

~~~
phaer
...or you are trying to build extra robust systems. And to do that, it helps
to choose carefully which libraries to use because one has to understand them
all. And then readability of the language is becomes an issue and I'd argue
that average readability varies between programming languages.

~~~
Aldo_MX
You don't say "I'm going to build an extra robust tool, let's start <?php
class ExtraRobustTool { ... }"

To build an extra robust tool you need a lot of planning, and a lot of factors
are considered during the planning.

------
jnardiello
PHP guy here. Which doesn't mean that I love php that much, but still pays all
the bills. How I ended up doing PHP? Long story. Anyway, i've been working in
almost any kind of environment. PHP powers massive and remarkable
architectures and i NEVER felt it was falling short during implementations.
Another story is the community, which is also the only damn community which
has scientists and amateurs sitting in the same room. The quality of the code
depends entirely on the person who is using the language, still I think that
actually allowing "non-programmers" to power their projects easily (despite
being badly designed, they still can fly) with PHP is a feature and not a bug.

~~~
wyager
>i NEVER felt it was falling short during implementations.

[http://c2.com/cgi/wiki?BlubParadox](http://c2.com/cgi/wiki?BlubParadox)

>the only damn community which has scientists and amateurs sitting in the same
room

I see scientists of all sorts in #python and #r, and scientists of the more
theoretical sort in #haskell quite frequently.

~~~
jnardiello
As far as i can see, smart people of HN is failing to grasp the meaning of a
simple sentence.

You won't see _ANY_ amateur using R. Period. In the PHP community you find
people building hairdressers websites and distributed systems experts. It's
not about having scientists or having amateurs, is about having them together,
at the same time and in the same room.

As a side note: Blub paradox doesn't apply.

~~~
imanaccount247
>You won't see ANY amateur using R. Period

What? I see tons of them. I am one of them. What kind of ridiculous assertion
is that?

>It's not about having scientists or having amateurs, is about having them
together, at the same time and in the same room.

It is about you pretending something is unique when it applies to every single
programming language in existence.

------
debacle
I agree for the most part. There are a few infuriating things, especially
coming from a more functional background, that drive me crazy. People who shit
on PHP aren't being objective.

------
Shish2k
My mind is honestly blown by people here thinking that half-assed type
checking, local variables, and classes are novel and unique features of PHP
which make it better than most other languages. Am I really living in a world
where people try PHP, and try Javascript, and consider themselves well-
informed expert programmers who have seen all there is to see? :S

A perfect metaphor for me reading this thread:
[https://i.imgur.com/fKX2Xoh.png](https://i.imgur.com/fKX2Xoh.png)

------
ChikkaChiChi
PHP is a good language for solo devs that need to get shit done quickly. PDO
is fast and stays out of the way.

------
krapp
I think one of the fundamental points that a lot of criticism of PHP misses
(and there is a _lot_ to criticize about PHP) is that it doesn't have to be a
very good language to do what it needs to do, which is, almost entirely, CRUD.

Everything else aside, being able to get an iterable array from a database and
crap out an HTML page with very little effort says a lot about its popularity.
You don't need to be a good language to accomplish that, or even really a sane
one. You can mix css, javascript and data sets together right in the same
document. Insane? Probably. A bad idea? Almost always. But also weirdly
liberating, and very easy to get from point A to B.

------
hyp0
The brilliant thing about PHP is webapages as templates. It's so simple and
easy to get something useful going, both to write it and deploy. Templates
weren't _new_ \- shell had string interpolation - just simple and useful.

The libraries and improvements to the language follow from its popularity,
which followed from its simplicity and usefulness.

------
icedchai
What I like about PHP: ease of deployment. No builds or process restarts
required. This makes it easy to prototype and rapidly iterate.

~~~
alexbilbie
Well that isn't necessarily true. If you're deploying new code then you need
to update the web server to point a new directory then that process needs
restarting (or at least soft-restarting).

If you're redeploying into the same directory then you should be at the very
least clearing the opcache so that old code lingering around in memory isn't
served up.

~~~
icedchai
Not really. My point is you don't need to do any of that during development.

And for most small projects (which are most of them, really) you don't need to
do it in production, either.

------
meowface
The only thing I agree with here is that it is indeed easy to get started with
PHP. It's also easy to get started with BASIC. You should not be using either
to write a serious application in 2014.

~~~
k1kingy
Why exactly? Why shouldn't you use PHP in 2014?

~~~
SapphireSun
As an author of a PHP app, it's because there are better languages with better
defaults out there. I don't understand why you would use a language with
clunky libraries and hacked modules when you could use python, ruby, or node
(amongst other great choices).

Why would you choose something kinda grimy when you don't have to?

------
imanaccount247
This isn't radical. PHP apologism is incredibly widespread. And it almost
always takes the same poor form you chose here, of pretending everyone is just
conflating preference with quality. No, supporting octal constants only by
accident, and doing so incorrectly because it was an accident is not a valid
design decision and simply a difference of opinion. Plenty of other languages
have been used as much or more than PHP, without the same level of criticism.
Consider the very real possibility that some of that criticism is valid and
try reading it instead of dismissing it.

------
Dewie
> Sure, PHP has its warts — every language does.

Some more than others.

------
arenaninja
I happen to agree, so bring on the downvotes

Some of the PHP criticism is valid, and most of it is valid if we're talking
about pre-composer PHP. But with composer, namespaces and traits, PHP can be a
lot of fun. Sure, there's no real concurrency support, but so far I'm able to
live with that. It's also weakly typed, but there's a LOT of web apps that
don't need strong typing. The documentation is a good starting point, but some
of the warts will come out in edge cases. The only thing I'm mad about is
needle/haystack. I also wish string and array slicing were as easy as they are
in python

