2

Of the following, which conditional statement is best practice:

if ($_GET['type'] == 'user' || $_GET['type'] == 'salesmen')

or

if ($_GET['type'] == ('user' || 'salesmen'))

Thanks for your help.

scarhand
  • 4,269
  • 23
  • 63
  • 92

6 Answers6

6

Always use the first one:

if ($_GET['type'] == 'user' || $_GET['type'] == 'salesmen')

The second one is a valid PHP ststement but it will produce unexpected results ( Not the output you are expecting ).

Chethan N
  • 1,110
  • 1
  • 9
  • 23
  • 2
    Alternatively, I've created an array with the elements 'user' and 'salesmen', then used a conditional using the in_array() function. Thanks for your answer. – scarhand Feb 24 '14 at 10:07
  • That should also work, that method is usually used when you have to compare with many values. You are welcome – Chethan N Feb 24 '14 at 10:09
  • 3
    The second one will not produce “unexpected” results, the result is well defined in PHP – it is just not what the OP _wants_. – CBroe Feb 24 '14 at 10:10
4

Have you tried it? One simple try would point you that the second part will not work as you expected.

However, I would suggest in_array()

if (in_array($_GET['type'], array('user', 'salesmen'))) {
   //
}

or

$type = array('user', 'salesmen');
if (in_array($_GET['type'], $type)) {
    //
}
Royal Bg
  • 6,988
  • 1
  • 18
  • 24
  • It's worth noting that in PHP5.4 they added short arrays so this is even cuter: `if ( in_array($_GET['type'], ['user','salesmen']) ) {` – Mousius Feb 24 '14 at 10:13
2

The first one is correct. In the second one you are actually comparing $_GET['type'] with a boolean expression ('user' || 'salesmen')

hidro
  • 12,333
  • 6
  • 53
  • 53
2

In your second example

('user' || 'salesmen')

Will be evaluated first, which results in a boolean true. Resulting in the following expression to be evaluated for the first:

$_GET['type'] == true

Which may or may not be true. In this case that would be true if $_GET['type'] is 1, true, an object, a string, or anything else that evaluates to true. This aspect is hidden in your question, but I would like to highlight that it could be prevented by using the identity operator (===).

Even prettier (imho):

$typeIsUser = $_GET['type']  === 'user';
$typeIsSalesMan = $_GET['type'] === 'salesmen';
if ($typeIsUser  || $typeIsSalesmen){
...
}

Or use in_array! The possibilities are endless.

Community
  • 1
  • 1
Spork
  • 1,631
  • 1
  • 21
  • 37
1

The first option is the valid one, the second one is not doing what you think it is.

You can find a full discussion of the various options to achieve this here:

Compare multiple values in PHP

Community
  • 1
  • 1
Tim B
  • 40,716
  • 16
  • 83
  • 128
1

The first thing you will want to do is make sure the parameter exists, otherwise you may see notices about a missing array index with a URL like /page?foo=bar:

if (isset($_GET['type'])) { ... }

Combining that with the rest of your code:

if (isset($_GET['type']) && ($_GET['type'] == 'user' || $_GET['type'] == 'salesmen')) { ... }

Alternatively:

if (isset($_GET['type']) && in_array($_GET['type'], ['user', 'salesmen'])) { ... }

Btw

This code:

if ($_GET['type'] == ('user' || 'salesmen'))

Compares $_GET['type'] with true, yielding true for most values.

Ja͢ck
  • 170,779
  • 38
  • 263
  • 309