1

Reading this question I want to copy @Your Common Sense's error checking when using mysqli

$query="INSERT INTO testtable VALUES (23,44,56)";
$stmt_test->prepare($query);
$stmt_test->execute() or trigger_error($stmt_test->error); 
$stmt_test->close(); 

How does or work? Another example of it's use is

$fh = fopen($myFile, 'w') or die("can't open file");

How is it different than using an if statment and would should it be used instead?

Community
  • 1
  • 1
Celeritas
  • 12,953
  • 32
  • 95
  • 174

2 Answers2

2

If the first statement returns false, then the second one is executed. That's it.

MurifoX
  • 14,374
  • 3
  • 34
  • 59
  • Note that it doesn't have to be a boolean false, it can be an empty string, "non-truthy" value, etc. Same as `if (condition)`. See: http://php.net/manual/en/types.comparisons.php – Wesley Murch Aug 12 '13 at 20:19
  • @WesleyMurch An empty string actually evalutes to `false`. So in general, any expression which evalutes to `false` will trigger the second expression to be run. – ComFreek Aug 12 '13 at 20:20
  • 1
    @ComFreek right but it doesn't actually "return `false`", if the condition/variable would return an empty string, it is then loosely evaluated to "not true". `'' === false` is not true ;) Just trying to clarify for the OP. – Wesley Murch Aug 12 '13 at 20:21
  • Isn't this unnecessary confusion, why not use an if statement? For code compactness? – Celeritas Aug 12 '13 at 20:23
  • @Celeritas It is just style preference. And as is evident, it is not as clear as an if statement to some people, but the compactness of the code may be clearer or more readable to others. I'd avoid using it until you feel you fully understand it. – Wesley Murch Aug 12 '13 at 20:24
  • @WesleyMurch I disagree... do this or do that seem very readable to me. – Orangepill Aug 12 '13 at 20:25
  • @Orangepill I have noted that in my comment. My point was that it may confuse those with less experience (as is evident here). – Wesley Murch Aug 12 '13 at 20:25
  • @WesleyMurch Once devs get used to it, i think it becomes very clear. – MurifoX Aug 12 '13 at 20:27
  • @MurifoX Exactly as I have said. – Wesley Murch Aug 12 '13 at 20:28
  • @WesleyMurch I will concede that fact... I guess until you realize what's going on under the hood it is a little like voodoo – Orangepill Aug 12 '13 at 20:28
  • What is this's name? Or statement? – Celeritas Aug 12 '13 at 21:05
  • @Celeritas This is very common in PHP and Perl, you may choose not to use it, but you won't stop the rest of the community. It's easy enough after you get used to it. – Juan Mendes Aug 12 '13 at 21:17
0

This is often how boolean "or" expressions are evaluated in programming languages. If you have a statement involving functions thus:

if (a() or b()) { ... }

then PHP works out that, if a() returns true, there is no need to evaluate the second function, since the overall result will be true regardless of the outcome of the second part. We can use this trick as a simple if mechanism:

(operation_that_might_fail() or report_error());

Here, I've removed the if around the clause. This will be evaluated just as before - except the result of ORing the two is then thrown away, since we don't need to do anything with it.

For this to work, operation_that_might_fail() must return boolean true on success, and false otherwise. As it happens, many PHP functions do exactly that, so we can often use this approach.

Side note: whilst the statement or is arguably clearer, PHP programmers tend to prefer the operator ||. Similarly, and will do what it says, but && is more common.

halfer
  • 18,701
  • 13
  • 79
  • 158