-1

I just wondering below code ! I had never seen and heard before .Why string date is equal to 0 ? Is there any documentation for that..

<?php
$p = "date";
$n = 0;
$m = 1;
var_dump($p == $n);//true
var_dump($p == $m);//false
var_dump($n == $m);//false
?>
Jack jdeoel
  • 4,554
  • 5
  • 26
  • 52

3 Answers3

0

Yes, you compare string with int so string is converted to int first. int from "date" string is 0

nospor
  • 4,190
  • 1
  • 16
  • 25
0

That's how it works:

Reference : Manual [See the table]

Loose comparisons with ==
"PHP" == 0 is true
"PHP" == 1 is false

Strict comparisons with ===
"PHP" === 0 is false
"PHP" === 1 is false

So is your case with "date"

Vijay Dohare
  • 731
  • 5
  • 22
Thamilhan
  • 13,040
  • 5
  • 37
  • 59
0

See this

you have used ==

0 is an int, so in this case it is going to convert 'date' into int. Which is not parseable as one, and will become 0. that is why you are getting true. try === opertor

Passionate Coder
  • 7,154
  • 2
  • 19
  • 44