What Not To Do

I saw this on thedailywtf.com the other day, and it bears repeating.

This is a severe anti-pattern in php:
$fp = fopen($filename, 'r');
while (!feof($fp)) {
$data .= fread($fp, 4096);
fopen(); returns null and prints to stdout on failure. feof(null) returns false because php os functions return false and print to stdout on error.

The net result is the above code turns into an instant infinite loop.

And this anti-pattern is clearly visible in the PHP manual.

Updated: typo fixed

No comments: