0

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')
1
  • @Mike, Your link to the manual says to use this: "$d = (true or foo());" -- it doesn't say to write the $var twice as in the answers below. Commented Sep 27, 2013 at 19:09

4 Answers 4

3

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')
Sign up to request clarification or add additional context in comments.

Comments

2

Try this

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

Comments

1

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.

Comments

0

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 !=.

2 Comments

false. PHP does support or directly, but it binds at a lower precedence than ||.
@MarcB I added a little on the or operator. However, what the OP is looking for is more on the way his statement should be written (which you do not even cover in your answer)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.