3

I have a small code snippet has below

$duration = 0;
if ($duration == 'daily') {
    $duration = '+1 DAY';
}

echo $duration;

I am expecting 0 has output but end results are +1 DAY. i am not able to understand why above conditions is success?

But we i use === returning expected results.

I have checked in php 7 and php 5.5. both versions are giving the 0 as output.

sankar.suda
  • 1,097
  • 3
  • 13
  • 26
  • 1
    Sounds like you're running into some kind of type-juggling issue. Basically it's trying to cast "daily" to an integer and getting 0, which matches integer 0 (http://stackoverflow.com/questions/4098104/odd-behaviour-in-a-switch-statement). Also, as a general rule it's better to put the constant/function call/thing that can't change on the left hand side of a comparison on the grounds that you can't accidentally assign it if you omit an = – GordonM Apr 14 '16 at 11:39
  • Php doesn't know to compare `string` ('daily') and `integer` (0) values so the if statement would always be true and `$duration = '+1 DAY'`. – Or Med Apr 14 '16 at 11:41

1 Answers1

0

This is type misunderstanding.

use,

$duration = '0';
if ($duration == 'daily') {
    $duration = '+1 DAY';
}

echo $duration;

It will show your answer.

HackerGK
  • 360
  • 3
  • 15