I have an if statement that is always evaluating true even if none of the conditions are true. It goes like this:

if(condition1 || condition2 || condidion3)
{
//do stuff
}

For whatever reason even if none of the 3 conditions are actually true, it just runs anyway. Is there something weird in or statements that cause this? I have even tried putting each condition in parentheses just to see if it helps.

Dani AI

Generated

A few common causes for an OR-expression to seem to "always be true" were already hinted at by . The usual suspects are accidental assignment inside the condition, truthy/falsy coercion, PHP operator-precedence oddities (the low-precedence or vs ||), and simple typos or uninitialized variables. The short checklist and examples below help isolate which one it is.

Quick checklist:

  • Check for assignment (=) used where a comparison was intended; assignment expressions evaluate to the assigned value.
  • Print value and type when in doubt: use var_dump()/print_r() in PHP, and console.log() plus typeof in JS.
  • Prefer strict checks when appropriate (===) to avoid coercion surprises.
  • In PHP, avoid mixing assignment and the low-precedence or; use || or parentheses to make intent explicit.
  • Remember language differences: PHP and JS have different rules for what counts as falsy (for example, the string "0" is falsy in PHP but truthy in JS).

Small examples that illustrate two common pitfalls:

<?php
$a = false || true;   // $a becomes true
$b = false or true;   // $b becomes false
var_dump($a, $b);
?>
console.log(0 || 'fallback');      // logs 'fallback'
console.log('0' || 'fallback');    // logs '0'  (string "0" is truthy in JS)

For reference, see the language docs on truthy/falsy behavior and PHP operator precedence: MDN: Falsy values and PHP operator precedence. Running each operand separately and checking both value and type usually reveals the mistake quickly; linters or small unit tests catch many of these issues before they appear in conditionals.

well, that means one of the conditions is definitely true..

Try to echo each condition variable and see which one is the one always true :) this is the simplest way

Gah, was a dumb error on my part. Gotta love mondays. Thanks yo.

you bet ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.