2

Why does this happen?

<?php $result = true ;
    if($result == 'SOME TEXT')
        echo 'why is this true? why is this printed?';        
    else
        echo 'when this should be printed?'
?>

Please explain, I am not looking for alternative method.

ro ko
  • 2,906
  • 3
  • 37
  • 58

5 Answers5

4

In your expression, you are comparing a boolean with a string. The string gets coerced to true.

Reference: http://www.php.net/manual/en/language.types.boolean.php#language.types.boolean.casting

As stated before, use === to compare both value and type.

Marc Audet
  • 46,011
  • 11
  • 63
  • 83
1

Because you use == operator instead of ===. In your case == doesnt rely on operands type, instead of ===

kirugan
  • 2,514
  • 2
  • 22
  • 41
0

It's because of PHP's auto type conversion; and since you're not comparing by type as well (===), it's considered TRUE. Here is a list of what is considered empty, but it's also considered FALSE:

The following things are considered to be empty:

"" (an empty string)

0 (0 as an integer)

0.0 (0 as a float)

"0" (0 as a string)

NULL

FALSE

array() (an empty array)

$var; (a variable declared, but without a value)

Everything else is basically considered TRUE.

Community
  • 1
  • 1
Shomz
  • 37,421
  • 4
  • 57
  • 85
  • Sometimes I ask myself why I even bother... – Shomz Mar 17 '13 at 15:14
  • @Shomz hehe harsh indeed. Guess asked wrong question on wrong day. – ro ko Mar 17 '13 at 15:25
  • The statement everything else is considered true is correct but I would say it confused people and that's why the vote down. Should say when evaluating, 'whatever string' == true or true == 'whatever string', the outcome will always be true. It's like saying `if ('some string')` which unless the string is empty... or '0'..., will evaluate to true. Just be more clear in your answer. – zgr024 Mar 17 '13 at 15:26
  • @zgr024 ... or unless the string is '0' (should I downvote you for that?). That's why it says **basically** so that anyone with any sense of logic would be able not to take it literaly... – Shomz Mar 17 '13 at 15:29
  • Edit your answer to include "Everything else is basically true when using ==".. and I'll upvote – zgr024 Mar 17 '13 at 15:29
  • 1
    I'm not buying reputation points... so do whatever you want. – Shomz Mar 17 '13 at 15:31
  • Buying??? I simply stated I would reverse my down vote which I can't do unless you edit. – zgr024 Mar 17 '13 at 15:32
  • Don't worry about it. – Shomz Mar 17 '13 at 15:33
0

I took a look at the php operator documentation..

In your case, you are saying with == that a boolean value of "true" is equal to a text value of 'SOME TEXT' but are NOT of the same data type. PHP returns true in this case because if just assumes they are similar if you disregard the data type.

If you used === then it would consider the data type and therefore will produce a value of false.

ecirp
  • 145
  • 2
  • 4
  • 11
0

Because you are comparing a boolean with a string, the string will be auto-converted to a boolean.

According to the PHP-documentation, any string that is not null or empty (or zero) will be converted to True.

So your comparison is like True == True.

If you use the === operator, you are also checking that both values are of the same type.

http://www.php.net/manual/en/language.types.boolean.php#language.types.boolean.casting

J. Sipkema
  • 68
  • 1
  • 6