Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which conditional statement is correct?

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.

like image 653
scarhand Avatar asked Jun 17 '26 22:06

scarhand


2 Answers

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 ).

like image 109
Chethan N Avatar answered Jun 19 '26 11:06

Chethan N


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)) {
    //
}
like image 26
Royal Bg Avatar answered Jun 19 '26 11:06

Royal Bg