-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
?>
David Jaw Hpan
  • 4,511
  • 3
  • 24
  • 50

3 Answers3

0

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

nospor
  • 4,150
  • 1
  • 14
  • 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
  • 699
  • 5
  • 21
Thamilhan
  • 12,752
  • 5
  • 35
  • 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
  • 6,650
  • 2
  • 16
  • 36