some1
some1

Reputation: 1737

PHP "or" syntax statement

Moving over to PHP from another language and still getting used to the syntax...

What's the proper way to write this statement? The manual on logical operators leaves something to be desired..

if($var !== '5283180' or '1234567')

Upvotes: 0

Views: 110

Answers (4)

UnholyRanger
UnholyRanger

Reputation: 1971

To build on the others, as the OP mentioned they are new to PHP, there is a couple things to be considered.

First off, the PHP or that you're looking for is the double line (||) and each item must be a statement on each side of the ||.

if ( $var !== '5283180' || $var !== '1234567')

addition:

As mentioned in the PHP Manual

The or operator is the same as the || operator but takes a much lower precedence. Such as the given example (from manual):

// The constant false is assigned to $f and then true is ignored 
//Acts like: (($f = false) or true) 
$f = false or true;

Now as mentioned, there is the general comparison (== or 'Equal') and the type comparison (=== or 'Identical'), with both having the reverse (not). In the given example, the test will check that $var is not identical to the values.

From PHP Manual:

$a !== $b | Not identical | TRUE if $a is not equal to $b, or they are not of the same type.

With this said, double check that this is what you're actually trying to accomplish. Most likely you're looking for !=.

Upvotes: 0

Marc B
Marc B

Reputation: 360592

PHP's or functions identically to the normal ||, but has a lower binding precedence. As such, these two statements:

$foo = ($bar != 'baz') or 'qux';
$foo = ($bar != 'baz') || 'qux';

might appear to be otherwise identical, but the order of execution is actually quite different. For the or version, it's executed as:

($foo = ($bar != 'baz')) or 'qux';

- inequality test is performed
- result of the test is assigned to $foo
- result of the test is ORed with the string 'qux';

For the || version:

$foo = (($bar != 'baz') || 'qux');

- inquality test is performed
- result of test is ||'d with 'qux'
- result of the || is assigned to $foo.

Upvotes: 1

Parixit
Parixit

Reputation: 3855

Generally, comparison is by using == and the reverse is !=. But if you want to compare values along with its data type, then you can use === and the reverse is !==.

Please refer to the documentation for more information.

You can use the following:

if($var!='5283180' ||  $var!='1234567')

Upvotes: 3

Sumit Bijvani
Sumit Bijvani

Reputation: 8179

Try this

if($var != '5283180' || $var != '1234567')

Upvotes: 2

Related Questions