views:

83

answers:

3
+1  Q: 

PHP compare doubt

if(0 == ('Pictures'))
{
  echo 'true';
}

why it's giving me 'true' ?

+3  A: 

Your string will be evaluated as an Integer, so becomes 0, use this : 0 === 'Pictures' that verifies identity (same value and same type)

Serty Oan
A: 

Use:

if (0 === 'Pictures')
{
  echo 'true';
}

The === is strict type operator, it not only checks the value but the type as well.

Quick Test:

if(0 == 'Pictures')
{
  echo 'true';
}
else
{
  echo 'false';
}

outputs true but:

if(0 === 'Pictures')
{
  echo 'true';
}
else
{
  echo 'false';
}

outputs false

Sarfraz
+2  A: 

Check PHP type comparison tables to understand how comparison operators behave in PHP.

In your case, 'Pictures' becomes "0" and therefore 0 = 0.

Let's check following example:

echo (int)'Pictures'; // 0 => 'Picture' as int
echo 0 == 'Pictures'; // 1 => true, 0 = 0
Ondrej Slinták