Why PHP Sucks

2013 年 11 月 8 日2,2900

Why PHP Sucks

“Recalling the exact syntax for the built-in stab() function, you make a sane assumption and call shoot(GUN,FOOT); The foot shoots your gun.”

— Some user who calls theirself “plams” on the Something Awful Forums

This is a list of reasons I don't like PHP.

Some reasons involve actual deficiencies in PHP; others involve features

that other programming languages provide that PHP has no reason not to

provide.

When I speak of "other languages", I'm thinking primarily Perl, Ruby, and

Python, that sort of thing.

Common Arguments In Favor of PHP

“PHP is easy to deploy.”

True, but once you select a programming language and templating system,

getting it installed on a system shouldn't take any more than an hour. And

I'm including worst-case scenarios here.

And this is a one-time operation. If you have to do it on a few hundred

machines, it can easily be automated. When hundreds if not thousands of

man-hours are going to be spent doing actual coding, is the time you spent

installing a programming language and templating system really an issue?

“PHP is easier to pick up than Perl/Python/Ruby/etc.”

Picking up the basics of just about any language isn't that hard. Picking

up proper coding practices that promote maintainability, security, simplicity,

resiliency, etc. in any language requires time and experience. Most coders

who have this kind of experience will have no problem picking up a new

language. Most coders who don't have this kind of experience will write

crappy code for a while, no matter what language they use.

One thing about PHP's documentation. It's

incomplete. User-provided comments in the online documentation are frequently

inaccurate. Arguably, this actually hinders one's ability to learn how to

write proper PHP.

“More people know PHP than Perl/Python/Ruby/etc.”

This is probably true, but do more good programmers know PHP than

Perl/Python/Ruby/etc.? If they can't pick up other languages, do you really

want to hire them?

“PHP makes it easy to build web sites!”

a/k/a

“PHP provides templating and embedding of code with HTML built-in!”

Let's throw aside for a moment the argument that this is not

a good feature for building larger systems because it encourages the

mixing of business logic and presentation code (though you can easily

develop habits to avoid this).

This is arguably true, but systems that provide templating and

embedding of code in HTML exist for other languages.

It's just that PHP is the only one that provides such a system

built-in.

There are many based on Perl. I'd choose Mason, but there are Template Toolkit, and millions

more.

For Python there is Spyce.

Specific Reasons Why PHP Is Inferior to Other Languages

The ternary operator

In most programming languages the ternary operator is right-to-left

assocative. This allows you to write things like this:





print((foo == 1) ? "uno" : (foo == 2) ? "dos" : "tres");



print("\n");



This chunk of code obviously outputs the word uno.

PHP's implementation of this operator has left-to-right. I dare you

to guess what the following PHP snippet outputs:





<?php



$foo = 1;



print(($foo === 1) ? "uno" : ($foo === 2) ? "dos" : "tres");



print("\n");



# (I use the === operator out of good habit.)



?>



Hint: it's not the word uno.

The || and && operators

In non-retarded programming languages, they return either the result

of the left operand or the result of the right operand.

In PHP, 5 || 4 returns 1. 0 || 6 returns 1.

While most people use || as a "default value" operator:





$foo = $bar || "default value";



this cannot be done in PHP.

Invoking external programs

PHP does not provide multi-argument forms of system(), exec(), or

other built-in functions. There is the pcntl extension, which is not

enabled by default.

Higher-Order Programming

If you're forced to use a version of PHP older than 5.3, you don't

get closures.

Other Issues

In PHP, .

Is it isset() or is_set()?

Is it isnull() or is_null()?

Is it sort() or array_sort()?

Is it merge() or array_merge()?

Even I don't remember. I have to look up the PHP documentation to answer

these. I'll let you do the same.

The arguments to built-in functions are

inconsistent.

The built-in functions' return values are

inconsistent.

The number of built-in functions is too

large. Do we really need 11 sort functions

(scroll down)?

Function names are case-insensitive, but variable names are

case-sensitive. WHY?

As I've , the vast majority of

official and user-contributed documentation is poor.

PHP's authors have made a few

non-backward-compatible

changes to the language. Not just between PHP4 and PHP5, but in minor

point releases of PHP4.

Magic Quotes

There is also the fact that PHP ever had the magic_quotes feature in

the first place. While it's been turned off by default for quite some time,

and will be removed from PHP 6, it will still affect you for some time to come

in the following situations:

    When writing programs to distribute to other people who may be running

    PHP4 or PHP5. Such as yet another forum or CRM package.

    When writing programs you may port from one operating system to another

    someday. Even if both are running the same version of PHP...

...You see, there's one particular PHP feature called magic_quotes_gpc,

where things get "interesting". It is on by default in PHP4. It is off by

default in PHP5 on certain Linux distributions; on in others. You can

probably stick "php_flag magic_quotes_gpc" off in apache.conf or .htaccess.

But some shared-hosting providers make this impossible. In the worst case,

you may very well have to pollute your code with things

like this. (There are common techniques such as

ereg_replace("/\\/","") that a lot of programmers use, which do

not work right in all cases.)

If You Must Use PHP

Advanced PHP Programming

by George Schlossnagle should be required reading.

When doing database programming, learn how to use prepared statements. It

will make your code more secure, and you will soon discover that you are much

happier. Chances are, you will need to use an abstraction layer such as PEAR Database or

ADOdb or

PHP Data Objects. PHP5's mysqli_*

functions also support prepared statements, but there is almost never a reason

to use RDBMS-specific functions.

Start using a templating engine. Use it to separate your business logic

from your presentation. You will discover that you are happier when you do

this. For most projects, you don't need a large package like Smarty. This

public-domain system in 40 lines of PHP code will do you just fine.

Other Anti-PHP Sites

PHP: a fractal of bad design

PHP Must Die

The Nature of PHP's Suck (lispcast.com)

— Actually pretty even-handed, but still comes to the correct

conclusion (that PHP sucks).

Problems with PHP (toykeeper.net)

Rackspace's PHP failure story

6 things that killed Waifmail — one of them is PHP.

Why PHP is good but bad

PHP in contrast to Perl

I'm sorry, but PHP sucks!

I hated php back when it was cool

What I don't like about PHP

Why I hate PHP (Google cache)

I hate PHP

Experiences of Using PHP in Large Websites

Things I Like About PHP

The fact that within the body of a function, variables are

local by default. You have to use the global keyword

if you want to actually modify global variables. No other

language in widespread use, to my knowledge, has this feature.

It was definitely borne of wisdom.

Footnotes

1 Python does have one rather

silly limitation on anonymous lambda functions: they can only

use one line and can only contain a single expression, not a series of

statements. Nested named functions are first-class objects though;

they're not anonymous, but they'll work. List comprehensions are

also useful, but I haven't much experience with Python, nor with list

comprehensions in any language.

0 0