In this context, or in this expression is one of the PHP logical operators that, when used, executes the second operator if and only if the first is due to a short circuit error.
Since fopen returns false, the or exit statement is triggered because the first part failed.
To understand this better, here is a short description of the short circuit rating.
$x = 5; $y = 42; if ($x == 5 or $y == 42) { echo "x or y is true"; }
In the above code, the expression $y == 42 never evaluated because it is not necessary because the first expression was true.
In this example, they use the same logic to decide whether to evaluate the statement that calls exit .
To ask your questions:
- I would not use exit when opening a file if the program was very specific. It would be best to register the error and then return the error to the caller so that they can decide what to do.
- When to use
exit completely depends on the code you write. - Given the explanation of the short circuit, yes, they are interchangeable in this sense. Using
or exit slightly shorter than using if/else .
Hope this helps.
drew010
source share