-1

is this a real condition? why it doesn't work for me in PHP? Javascript:

var miString = 'hello';
if(miString == ('hello' || 'hola' || 'ciao' || 'merhaba')){
     alert('Welcome');
}

in PHP:

if($_SERVER['REQUEST_URI'] == ('index' || 'post' || 'about')){
  // why this not work for me?
}
pharesdiego
  • 119
  • 5
  • it doesn't work because that's not how it works, period. You need to set those up in an array and check if one of those are in the array. – Funk Forty Niner Sep 24 '17 at 17:11

3 Answers3

1

You need to do every comparison completely, like this:

if($_SERVER['REQUEST_URI'] == 'index' || $_SERVER['REQUEST_URI'] == 'post' || $_SERVER['REQUEST_URI'] == 'about'){
  // why this not work for me?
}

Since, what you're doing now, is comparing $_SERVER["REQUEST_URI"] to true:

var_dump("hello" || "index"); // bool(true)

So the comparison is always true, because you're using only two equal signs, which is a loose comparison. Had you used ===, the result would be false because a string is not of the same type as a boolean.

Another way is to use an array and in_array(), which will check if a value is in the values of an array:

$values = ["index", "post", "about"];
if (in_array($_SERVER["REQUEST_URI"], $values)) {
ishegg
  • 9,685
  • 3
  • 16
  • 31
0

In JS, you're effectively doing this:

if (miString == 'hello') {

That is to say that the logical OR operators will give you the first truthy value, which in this case is 'hello', and so only that is used for the comparison.

JS doesn't have an operator shorthand for multiple comparisons. You can use multiple == operations, a switch statement, or some construct that searches items in a collection.

llama
  • 2,535
  • 12
  • 11
0

You're going to want to try something like this:

var miString = 'hello'; 
if(miString == 'hello' || miString == 'hola' || miString == 'ciao' || miString == 'merhaba'){
 alert('Welcome');
}
mrkmhny
  • 3
  • 1
  • 5