3

I'm a curious programmer. So these days I was reading the documentation from the PHP site and this link was "PHP type comparisons" http://www.php.net/manual/en/types.comparisons.php

I decided to do some exercises to fill the tables of comparisons but there are some answers that I can not see why, for example:

<?php
var_dump(false == array()); // Okay, an empty array is considered false. True result
var_dump('' == array()); // false ? Why not true if an empty string is considered false ?
var_dump(0 == array()); // false ? Why ?
var_dump(null == array()); // true. Why ?
?>

Can you help me about this? I can not understand why some comparisons, I can not find anywhere explanation.

kojiro
  • 74,557
  • 19
  • 143
  • 201
João Silva
  • 167
  • 2
  • 9
  • 1
    You are missing an equals sign in line 5, aren't you? – TimWolla Jan 01 '14 at 18:12
  • Php's equality logic is notoriously bizarre, but you should only ever compare like-objects anyway, using ===. Implicit conversions just aren't needed. – Dave Jan 01 '14 at 18:15
  • You are missing an `);` in line 2, aren't you? – oezi Jan 01 '14 at 18:15
  • 1
    You need to read abut [type juggling](http://php.net/manual/en/language.types.type-juggling.php) – John Conde Jan 01 '14 at 18:15
  • Check http://stackoverflow.com/questions/80646/how-do-the-equality-double-equals-and-identity-triple-equals-comparis, which has the same question – Alex Siri Jan 01 '14 at 18:15
  • 1
    @Alex they are not the same question. That question is *what*. This question is *why*. – kojiro Jan 01 '14 at 18:16
  • 1
    That question explains it perfectly. It's just type juggling which has been asked here ad naseum. Voting to close. – John Conde Jan 01 '14 at 18:17
  • @JohnConde It's possible one of the answers in that question explains it, but the question and its accepted answer do not. They simply list what PHP does. The other answers don't do much better at explaining the comparison between `array()` and other things. Even the php docs fall short. An explanation should either describe why the design decision was made or what the desugared core language does (or both). – kojiro Jan 01 '14 at 18:25

3 Answers3

1

Here is the reason why.

Case one will cast the array() to a boolean, resulting in false.

Case two and three are explained here, the scalars are cast to arrays:

For any of the types: integer, float, string, boolean and resource, converting a value to an array results in an array with a single element with index zero and the value of the scalar which was converted. In other words, (array)$scalarValue is exactly the same as array($scalarValue).

Case four is explained here:

Converting NULL to an array results in an empty array.

Bart Friederichs
  • 33,050
  • 15
  • 95
  • 195
  • So what you're saying is that `$x == $someArray` causes the value of $x to be implicitly cast to an array. To dig further, why does that happen? Why not implicitly cast the array to the type of `$x`? – kojiro Jan 01 '14 at 18:28
  • How would you cast an array to another type? – Bart Friederichs Jan 01 '14 at 18:30
  • About as arbitrarily as you would cast a scalar to an array. – kojiro Jan 01 '14 at 18:33
  • @kojiro agree. One of the reasons I am working on moving away from PHP. I reckon it is not possible, will introduce hard-to-debug mistakes and should cause (fatal) errors. – Bart Friederichs Jan 01 '14 at 18:35
  • Why the 2-3 case, both are transformed into an array with an element inside and not transformed to boolean? Why casting to array takes precedence? I thought the process was this: '' == Array () = (bool)'' == (bool) array () = true But the truth is this: '' == Array () = (array)'' == array () = array ('') == array () = false – João Silva Jan 02 '14 at 00:46
  • Continuing the above comment: I found the explanation for this in here http://au.php.net/manual/en/language.operators.comparison.php "Comparison with Various Types", but the documentation says it only just "array is always greater": Type of operand 1 - Type of operand 2 - Result bool or null anything Convert both sides to bool array anything array is always greater Myself would like to know why these priorities. – João Silva Jan 02 '14 at 00:47
1

It's all about type juggling, which type wins over the other type.

For instance, when you compare an number with a string, the number always wins so the string will be converted into a number. So "12abc" == 12 is true in PHP.

  1. When comparing a boolean (false) with something, that something is converted to a boolean. (bool) array() is false, so false == false is true.
  2. When comparing another value with an array, the other value is converted to array([0] => VALUE_OF_OTHER) (in other words, converted to an array). That means that the comparisation becomes array('') == array(), which is false
  3. Same as (2). array(0) == array() is false
  4. array(null) means just an array with nothing, thus array(null) == array() (which is the comparisation you did), so the result is true.
Wouter J
  • 41,455
  • 15
  • 107
  • 112
  • So why '' == array('') is false if u said " '' == array() " is the same as " arary('') == array() " ? In your theory it would be true . '' == array('') -> array('') == array('') -> true. But the truth is that this is not true – João Silva Jan 03 '14 at 07:20
  • @JoãoSilva "" will be converted to areay("") (that's an array with one element). That's not equal to array() (an array with zero elements) – Wouter J Jan 03 '14 at 08:51
0

Ok, so the question is WHY would you want this. This is to make this kind of comparisons easy:

$count = 10;    
while($count){
    echo $count;
    $count--;
}

This allows you to decide whether numbers that model a count, have anything or not.

The same thing happens with null, you can easily check if a variable has a value assigned or not.

In the case of arrays, it allows you to check wether the array is empty.

Alex Siri
  • 2,856
  • 1
  • 19
  • 24