2

In the file defines.php, I have define("YES",1);.

I am including this file in the login.php page as

require_once('/defines.php');

where I have this check if ($row['registered'] === YES). This is not evaluating to true. If I use double equals, it works. I did

echo $row['registered'];
echo YES;

and I am getting the output as

1
1

On my localhost machine, however, === is working fine. Why is this strange behaviour? Is there any dependency on production server?

PS : My hosing server is using PHP v5.4

EDIT

Var dump : string(1) "1" int(1) But I have tinyint type in database, why I am getting string data type?

Naveen
  • 7,944
  • 12
  • 78
  • 165

2 Answers2

1

Your database driver returns the value of registered as string

  • you can either typecast your integer values and them compare them using ===

    (int)$row['registered']

  • or use the == operator instead

Check this answer https://stackoverflow.com/a/80649/2255129 for the differences of === and ==

Community
  • 1
  • 1
nohponex
  • 58
  • 1
  • 7
0

the problem is

$var1 = '1';
$var2 = 1;

// this will be right
if ( $var1 == $var2 )
{ echo "'1' = 1"; }

// This will never be right
if ( $var1 === $var2 )
{ echo "'1' != 1"; }

in PHP you can not use === to match string and int or boolean.

eg.

1 == '1' = true

1 === '1' = false

1 === 1 = true

true === 1 = false

true == 1 = true

thats mean if you use === and not == its need to be same type like string === string or int === int

you need to use == if you want to match int and string, i do not recommend you combine string and int in a if statement.

convert you string to int, and make a match agin like this

$var1 = (int) $row['registered'];
$var2 = (int) YES;

if ( $var1 === $var2 )
{ echo 'your right'; }
else
{ echo 'you got a problem'; }
Community
  • 1
  • 1
ParisNakitaKejser
  • 12,112
  • 9
  • 46
  • 66