PHP nuisances
- Published February 18th, 2007 in PHP
PHP is probably the most annoying and pesky language I've come across. Here's my last catch up.
Today I was updating some legacy PHP code due to a bug consisting of a mail only being dispatched to one recipient while it should be sent to two different emails. I double checked the To: header to verify that it contained the emails separeted by commas (note that PHP parses the parameters of the mail() function so be sure to not include the emails in the form Name ). Everything was OK.
I then took a look at the mail source and noticed that the MIME type had a very weird character attached to it. An 'n'. Not that weird though. Header fields are separated by a carriage return and new line feed (\r\n) or, optionally, by a new line feed (\n). I looked at the code and it seemed OK.
-
$headers = 'From: Email <bla @mywheel.net>' . '\n';
-
$headers .= 'MIME-Version: [...]' . "\n";
What could be wrong then? Well, PHP ignores escape values if inside '' instead of "". This a "#$#" behavior. So I had to replace '\n' by "\n". Bug corrected.
P.S. - This reminds me of how cool other interpreted languages like Ruby and Python are. The world is a better place with them.




Well, that’s one of the first things I remember learning in PHP. Furthermore, it goes beyond escape characters. Like this:
$foo = 'batatas';echo "bife com $foo" //bife com batatas
echo 'bife com $foo' //bife com $foo
I don’t think it’s counter-intuitive by itself by it turns the code more difficult to read.
For instance, in Ruby while referring code inside a string one has to use
"some string #{var}"pointing immediately to the fact that it’s a variable and not a weird character.
“For instance, in Ruby while referring code inside a string one has to use(…)”
Well, but you may want to write a string with stuff that would otherwise be substituted, without obfuscating it with piles of escape characters.
But I agree that something like Python’s raw strings — r”some string” — is more readable, although the “”/'’ distinction is so common (starting with most shell languages) that I never even think of it. In fact, again, in Python ‘’ and “” mean the same, and I always find myself not linking to use ‘’ anywhere, just because it makes me immediately see a raw string there…
Carlos,
The distinction between ‘’/”" is quite common but it’s so subtile that makes it straight hard to notice bugs like this one. It’s not the first things I, as a programmer, want to notice: whether I used ‘’ or “”.
There are oodles of stuff to complain about in PHP. String escaping is not one of them. PHP’s behavior is the standard for most unix shells. If you think of it, unix shells were probably the earliest widely used dynamic languages.
Sérgio,
The fact of being standard for UNIX shells makes it intuitive or a right choice? I guess the example provided shows otherwise: it took me a couple of time debugging to figure out that I missed those ‘ ‘ (and yes, I was aware that PHP does not parse or escape values inside ‘ ‘). And most of syntax highlighters (if not all) do not make distinction of ‘’ and “”.
Following the same reasoning then Apple wouldn’t have created launchd amongst other things on the UNIX platform since they’re not standard.
PHP just copied a bad example of counter-intuitiveness.
Actually, I have to agree with Sergio here. PHP just did what PERL and Python did and adopted the same standard that UNIX programmers are used to regarding string quoting and escaping.
It’s not counter intuitive to me, and actually I would find it annoying that a dynamic language didn’t support string quoting this way.
If this sort of syntax scares you, you should use more explicit alternatives like the printf*() family of functions (or Python’s % string operator).
Mario,
I don’t agree with you, this is quite common behaviour, i code in PHP and in Ruby, and they behave the same way:
try it on ruby with irb:
puts ‘line 1\nline 2′
puts “line 1\nline2″
Andre
Duh. Is this the lamest rant you can get? complaning about quotes? As if raw strings nomenclature or dozens of escapes are a better solution. If you don’t know the basics, don’t rant about them please :P
Bruno,
I was aware of the basics. It’s just not the first thing running through your head while trying to find out a bug. It’s just a dumb decision, IMHO. For some reason “more evolved” languages like Ruby or Python have gone other way…
Exactly. “more evolved” is my grip.
Why is a r”" more evolved than a ‘’. A ‘ is not the same as a “, so it just makes sense to have them behave differently. Everyone (coming from unix) already knows the difference between a ‘, a “, and even a `, so why confuse those guys by doing it the “wrong” way and make everything the same (”=’), and then when they (the other languages) notice the mistake they caused, then they create new and strange ways to do what used to just work (like inventing r”").
And by the way, when an experient developer sees a literal \n (or any \xx) on the screen, the first thing that comes to ones mind is a search for ‘.*\\ on the code. :P
I’m not questioning the fact that’s common behavior on UNIX shells. I’m truly aware of that. But using the same behavior on a interpreted language for the web does not make sense to me.
It took me a couple of minutes to notice that I was using ‘’ instead of “”. It must have been a conditional reflex since I mostly use ‘’.
“something #{variable}” may have three more characters but I wouldn’t waste a fraction of the time I did looking throughout several lines of code looking for some ‘’.
Note: I’m done with PHP.
But in this case is a simple: PBCK ;)