2

Very strange question, I've never seen anything like that. Let's see the code:

$_POST['SCORE1'] = 4;
$_POST['SCORE2'] = 0;
var_dump ($_POST['SCORE1']);
var_dump ($_POST['SCORE2']);
var_dump ($_POST['SCORE1'] == '?');
var_dump ($_POST['SCORE2'] == '?');

it echoes the followings:

int(4) 
int(0) 
bool(false) 
bool(true)  ??? wtf ???

I dont understand the last one. 0 = ?

John Smith
  • 6,129
  • 12
  • 68
  • 123

1 Answers1

4

In this expression $_POST['SCORE2'] == '?') php converts the string ? to a numerical value. When it does convert the value becomes 0. So 0==0 is true.

DevZer0
  • 13,433
  • 7
  • 27
  • 51
  • 1
    interesting... after many years, I didnt experience that. Php has been doing it always? – John Smith Jul 13 '13 at 10:07
  • the same way POST global convers dotts `.` to `_` – samayo Jul 13 '13 at 10:07
  • yes its the loose type behavior if you compare a string with int that happens. – DevZer0 Jul 13 '13 at 10:09
  • @DevZer0 While it is true what you are saying I don't see a logic behind this. (12 years PHP exp) ;) It converts to boolean `true` but integer `0`. I'm currently considering to throw away all my PHP code and convert to java – hek2mgl Jul 13 '13 at 10:10
  • The number 0 is always equal to any string - doesn't make all that much sense, but === should be used most of the time anyway - at which point this issue does not occur – MDEV Jul 13 '13 at 11:23
  • @SmokeyPHP 0 isn't equal to any string. Try `var_dump('1' == 0);` – hek2mgl Jul 14 '13 at 22:30
  • @hek2mgl Well yes, any textual string of course - obviously a number cast as a string will be parsed as it's number and be compared correctly. – MDEV Jul 15 '13 at 09:51