0

i got into some trouble by using OR if one condition is true and the other false. in my constillation right now it is always only the second condition token.

if($num_rows === '0' OR $_GET['id'] !== $_SESSION['UserID']) 
{ echo "show me something"; }
else
{ show nothing; }

i get only always 'show nothing'.

Biffen
  • 6,249
  • 6
  • 28
  • 36
Kallmas
  • 97
  • 2
  • 16

5 Answers5

2

Note that === is strict comparison. I think the following:

$num_rows === '0'

should rather be:

$num_rows == 0

$num_rows is presumably an integer and not a string (a piece of text).

Related: PHP == vs === on Stack Overflow

Watch out with the second comparison, too:

$_GET['id'] !== $_SESSION['UserID']

Here, it's probably better to use != in favor of !== as well. $_GET is generally read as string, so even something like ?id=5 will return as string "5" and not as integer 5

Here's a quick test to illustrate:

if (isset($_GET['id'])) {
    echo '<h2>Comparison with == 5</h2>';
    var_dump( ($_GET['id'] == 5) );
    echo '<h2>Comparison with == "5"</h2>';
    var_dump( ($_GET['id'] == "5") );
    echo '<h2>Comparison with === 5</h2>';
    var_dump( ($_GET['id'] === 5) );
    echo '<h2>Comparison with === "5"</h2>';
    var_dump( ($_GET['id'] === "5") );
}
else {
    echo 'Please set an ?id to test';
}

This will output (notice the third item is false) the following with ?id=5:

Comparison with == 5

boolean true

Comparison with == "5"

boolean true

Comparison with === 5

boolean false

Comparison with === "5"

boolean true

Community
  • 1
  • 1
ljacqu
  • 2,132
  • 1
  • 17
  • 21
1

Probably because you use absolute equality with a string.Mysli_num_rows returns an int.

http://php.net/manual/en/language.operators.comparison.php

Equal and of the same type

Mihai
  • 26,325
  • 7
  • 66
  • 81
1

You dont need to use === as it is a strict comparison and $num_rows is probably an int Try this:-

if($num_rows == 0 OR $_GET['id'] != $_SESSION['UserID']) 

$_GET['id'] will provide you a string. You may chech the manual for details

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
0

To be really correct, need something like this:

if ($num_rows === FALSE)  {
    echo 'db resource/link gone';
}
elseif ($num_rows === 0 || intval($_GET['id']) !== intval($_SESSION['UserID'])) {
    echo 'no db result or id mismatch';
}
elseif (intval($num_rows) > 0 && intval($_GET['id']) === intval($_SESSION['UserID'])) {
    echo 'everything good';
}
else {
    echo 'something unhandled went wrong';
}
Daniel W.
  • 31,164
  • 13
  • 93
  • 151
0

thanks for all the answers. i got it to work. i change the order and the opreator to what you've suggested.

i made it like this:

if($num_rows > 0 OR $_GET['id'] == $_SESSION['UserID']) { echo "show nothing"; }
else { show me some      stuff }

so thanks again for all the help. :)

Kallmas
  • 97
  • 2
  • 16