Even more PHP mistakes!

Tags:

I recently wrote a post about three PHP mistakes that can give you a bad headache. The blog readers commented even more mistakes, and there was a whole new article on another site with 8 mistakes of their own!

This post is dedicated to the mistakes my readers posted in the comments, and lastly linking to the other mistake article.

Mistakes from the readers

Sam posted his mistakes:

$arr = array('A', 'B', 'C');
foreach ($arr as &$val) {}
foreach ($arr as $val) {}
var_dump($arr);

This is a curious one – The end result of the var_dump is A, B, B instead of A, B, C which you might’ve expected. But why is this?

The weirdness here is caused by the way PHP’s references and variable scopes work. After the first foreach, $var is actually a reference to the final index of the array! So when the second foreach loops through the array, it’s actually assigning the value of each index one by one to the final index. Because the 2nd is B, it will assign B to the 3rd index, and when it finally reaches the 3rd index, its value is B, so it finally assigns B to it.

Sam’s second mistake was PHP, UTF8 and the BOM – Byte Order Mark. Some editors include a BOM in the start of a code file if the file uses UTF8 encoding. This is to tell any applications reading the file that the file is using UTF8. The BOM should be ignored, but due to a bug in PHP, it may interpret it as output. This can cause issues for example with sending headers and can be quite difficult to find.

Reader Olli’s mistake:

$url = “http://foobar.com/”;
 
if (!strpos($url, “http”))
// returns true, because !0 => true
if (strpos($url, “http”) === false)
// returns false

This is caused by the way PHP handles == in a “non strict” fashion. Pretty obvious as it’s stated in the manual and all, but sometimes it can be easy to miss.

Pau Sanchez‘s mistake:

empty($v) when $v = “0″;

The behavior of PHP’s empty() function is a subject of hot debate. It can indeed cause problems if you don’t understand how it works.

nathan points out a good way to avoid mistakes:

I learned long ago to implement “is equal to” and “is identical to” comparisons with this syntax:

// is equal too
if (”foo” == $foo) {}
 
// is identical to
if (”foo” === $foo) {}

This helps avoid inadvertently assigning a variable the value of “foo” since:

if (”foo” = $foo)

will raise an error. This tip might be common knowledge but I thought I would mention it.

From what I have heard, this way of using the comparison string as the first in an if-clause comes all the way from C and C++, where you also could accidentally assign things in if-clauses.

And last but not least, Oliver Leitner’s favorite:

try to find out why something isnt comparing…

if($row[’something’] !=){
//do something
}

when the row returns ‘NULL’.

Never forget error_reporting(E_ALL|E_STRICT), which will point out if you attempt to access undefined arrays!

More mistakes on NETTUTS

NETTUTS recently posted an article “Are You Making These 10 PHP Mistakes?” which contains 8 more mistakes.

You may notice a comment from me at the NETTUTS page regarding the author not giving credit for two of the mistakes in the article. I had a quick chat with the author, Glen Stansberry, and he was prompt in fixing the issue. Thanks Glen!

Remember to read my original mistake post if you haven’t read it yet!