Using 'or die()' to stop on errors in PHP

Viewed 5276

Often in PHP, I see:

$result = mysql_query($query) or die();

Coming from python, I know why this should work, because or returns the first value if it is true in a boolean context, and the second value otherwise (see this).

But when I try the above technique in PHP in another context, for example something like:

$name = "John Doe";
echo $name or "Anonymous";

The or doesn't return the first value ("John Doe"), it returns 1.

Why does this work in the mysql_query() result case, but not in other cases? Is it bad to use in a mysql_query() case (ignore the fact that I am not returning a useful error to the user)?

7 Answers
Related