PHP "or" Syntax

Andrew picture Andrew · Mar 2, 2012 · Viewed 46.5k times · Source

I've seen this a lot: $fp = fopen($filepath, "w") or die(); But I can't seem to find any real documentation on this "or" syntax. It's obvious enough what it does, but can I use it anywhere? And must it be followed by die()? Are there any caveats to using or when you could use something like

if (file_exists($filepath))
   $fp = fopen($filepath,"r");
else
   myErrMessage();

I know it seems like a silly question, but I can't find any hard and fast rules for this. Thanks.

Answer

Smamatti picture Smamatti · Mar 2, 2012

It's a logical operator and can be used in any logical expression.

http://php.net/manual/en/language.operators.logical.php