-1

I am trying to apply some if else statements in my code but getting a bit confused. I want a result if I got value=0 answer should be One. if I got value=1 answer should be Two and if I got any value greater then 1 value should be Three. Following is my code: -

$value = 1
if($value < 1)
{
  echo "One";
}
else if($value === '1')
{
  echo "Two";
}
else if($value >= 2)
{
  echo "Three";
}

The problem is that it is not giving me results if value is = 1.

Imran
  • 37
  • 6

2 Answers2

1

change this else if($value === '1') to else if($value == 1)

Giorgi Lagidze
  • 773
  • 4
  • 24
0

=== matched both value and type that's why 1==='1' is false, See Ref.

$a === $b Identical TRUE if $a is equal to $b, and they are of the same type.

<?php
$value = 1;
if($value < 1)
{
    echo "One";
}
else if($value == '1')
{
    echo "Two";
}
else if($value >= 2)
{
    echo "Three";
}
?>

DEMO: https://3v4l.org/avOWc

A l w a y s S u n n y
  • 36,497
  • 8
  • 60
  • 103