0

I need one solution for this script. I don't want to remove the first zero because I show the example code only. In case I have received zero in my random function. This is my script.

<?php 
$var1 = '0123456';
$var2 = '123456';
if($var1 == $var2){
    echo 'Equal';
} else{
    echo 'Not Equal';
}
?>

In here I get the output is Equal But I need Not Equal. I know these two values are same, But am working on OTP So it should not allow this method. How can I do this, any Idea?

Thamilhan
  • 12,351
  • 5
  • 33
  • 59
Nawin
  • 1,505
  • 1
  • 12
  • 22

2 Answers2

3

Just use ===

<?php
$var1 = '0123456';
$var2 = '123456';
if($var1 === $var2){
    echo 'Equal';
} else{
    echo 'Not Equal';
}

=== - Checks both value and datatype (Strict comparison) - When you compare with this, both are considered as strings

== - Checks only value (Loose comparison) - When you compare with this, 0 will be dropped for comparison as it compares like integer values.


Know more about the equality from another SO answer

Thamilhan
  • 12,351
  • 5
  • 33
  • 59
1

Just Replace it With Your Script

   <?php
   $var1 = '0123456';
   $var2 = '123456';
   if($var1 === $var2)
   {
    echo 'Equal';
   } 
   else
   {
    echo 'Not Equal';
   }
   ?>
Dhyey Pathak
  • 29
  • 1
  • 1
  • 5