Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Your code is syntactical valid but semantical probably not what you wanted.</p> <p>Because <code>$var1 || $var2</code> is a <a href="http://php.net/language.operators.logical" rel="nofollow noreferrer">boolean expression</a> and always yields <em>true</em> or <em>false</em>. And then <code>$var</code> is compared to the result of that boolean expression. So <code>$var</code> is always compared to either <em>true</em> or <em>false</em> and not to <code>$var1</code> or <code>$var2</code> (that’s what you’re have probably expected). So it’s not a shorthand to <code>($var == $var1) || ($var == $var2)</code>.</p> <p>Now as you already noted yourself, <a href="http://php.net/in_array" rel="nofollow noreferrer"><code>in_array</code></a> is a solution to this problem if you don’t want to write expressions like <code>($var == $var1) || ($var == $var2)</code>, especially when you have an arbitrary number of values you want to compare to:</p> <pre><code>in_array($var, array($var1, $var2)) </code></pre> <p>Which is equivalent to:</p> <pre><code>($var == $var1) || ($var == $var2) </code></pre> <p>If you need a <a href="http://php.net/language.operators.comparison" rel="nofollow noreferrer">strict comparison</a> (using <code>===</code> rather than <code>==</code>), set the third parameter to <em>true</em>:</p> <pre><code>in_array($var, array($var1, $var2), true) </code></pre> <p>Which is now equivalent to:</p> <pre><code>($var === $var1) || ($var === $var2) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload