37

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.

Smamatti
  • 3,882
  • 3
  • 29
  • 42
Andrew
  • 2,838
  • 2
  • 28
  • 35
  • 1
    `or` can be used to combine *expressions*, not *statements*. And it's sometimes useful because of its lower operator precedence (in comparison to the assignment). Btw, your if blocks are missing curly braces. Don't look for syntax shortcuts until you've mastered that. – mario Mar 02 '12 at 14:57
  • 1
    Curly braces are unnecessary for conditionals followed by a single statement. – Andrew Feb 14 '13 at 23:09
  • 3
    Yes yes; the language syntax allows that. It looks amateurish and is commonly frowned upon nonetheless. – mario Feb 17 '13 at 23:02
  • 6
    I'd say that's a matter of opinion. I'd also argue that any (reasonable) formatting preference is a relatively poor indicator of what I have and haven't mastered, and is certainly no grounds upon which to dismiss my question until my tastes conform to yours. – Andrew Feb 19 '13 at 00:53

8 Answers8

15

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

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

Smamatti
  • 3,882
  • 3
  • 29
  • 42
  • 6
    You should also look at precedence as `$fp = fopen($filepath, "w") or die();` is different from `$fp = fopen($filepath, "w") || die();` – Michael Laffargue Mar 02 '12 at 15:23
  • 1
    To add some more info to the above: the first statement (using `or`) looks if the first expression `$fp = fopen($filepath, "w")` returns false (or anything that PHP considers equal to false, like `null` or `0`). If, and only if it does, it executes the second statement, `die()`. The second one does the same thing, but should usually only be used in an `if` context. – tschoffelen Aug 14 '13 at 18:42
14

Let's just say that:

$result = first() || second();

will evaluate to:

if (first()) {
    $result = true;
} elseif (second()) {
    $result = true;
} else {
    $result = false;
} 

while:

$result = first() or second();

will evaluate to:

if ($result = first()) {
    // nothing
} else {
    second();
}

In other words you may consider:

$result = first() || second();

$result = (first() || second());

and:

$result = first() or second();

to be:

($result = first()) || second();

It is just matter of precedence.

mpyw
  • 4,901
  • 2
  • 27
  • 32
tacone
  • 10,962
  • 8
  • 39
  • 59
6

This is neat trick, inherited from some PHP predecessor, based on the fact that PHP quite smartly won't evaluate any expression following OR, if first one returned true:

function a($ret){
    echo "FOO";
    return $ret;
}
function b($ret){
    echo "BAR";
    return $ret;
}

$result = (a(true) OR b(true));

will print out only FOO, means b() weren't even executed.

Your Common Sense
  • 152,517
  • 33
  • 193
  • 313
5

or just does a boolean comparison.

What's returned by fopen() can be treated as such a boolean value, because it returns FALSE if it fails (and a different value if it does not).

If it fails, the statement is evaluated to the right, and so the function die() is called.

Chris
  • 3,008
  • 24
  • 33
3

Basically it means "if the first command fails, then perform the second command." In your example, if PHP cannot open the file, it will terminate the script (die()).

SenorAmor
  • 3,241
  • 14
  • 27
  • Technically it tests if the first expression returns a boolean false, then with shortcut boolean expressions it will execute the second expression. If the first expression returns a non-false value, then shortcut boolean expressions mean there is no need to execute the second expression – Mark Baker Mar 02 '12 at 14:59
3

'Or' in PHP is like C-like syntax (||)

<?php 
if( ($a==1 || $a==2) && ($b==3 || $b==4) && ($c==5 || $c==6) ) { 
     //do that something here. 
} 
?>

The 'Or' you are talking about is just a trick as the following states:

Example:

$result = mysql_query('SELECT foo FROM bar', $db) or die('Query failed: ' . mysql_error($db));

The or die() trick is a very poor choice for several reasons:

  1. It's not a very nice way to present the user with an error message.
  2. You cannot catch the error in any way.
  3. You cannot log the error.
  4. You cannot control whether it should be output to the screen or not. It's okay to do that in a development environment, but certainly not in a production environment.

    5. It prevents you from doing any sort of cleanup. It just ends the script abruptly.

Reference: [enter link description here][1]

[1]: http://www.phpfreaks.com/blog/or-die-must-dieenter code here

Carlos Quintanilla
  • 11,470
  • 3
  • 19
  • 24
1

It can be used just like you'd use || as a logical OR http://php.net/manual/en/language.operators.logical.php

Exupery
  • 3,070
  • 4
  • 36
  • 48
0

It can be used as || but hasn't the same precedence: http://www.php.net/manual/en/language.operators.precedence.php

The precedence of an operator specifies how "tightly" it binds two expressions together. For example, in the expression 1 + 5 * 3, the answer is 16 and not 18 because the multiplication ("*") operator has a higher precedence than the addition ("+") operator. Parentheses may be used to force precedence, if necessary. For instance: (1 + 5) * 3 evaluates to 18.

Michael Laffargue
  • 9,799
  • 6
  • 39
  • 74