phpsyntaxprogram-flowcontrol-structureor-operator

PHP "or" Syntax


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.


Solution

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

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