1

I'm currently working on my first (small) PHP project going step by step teaching myself. It's going good so far but I do have a question about the following code.. and which I should use in which case..

equal:

if ($word1 != $word2) {
    echo "Your words do not match, please go back and correct this.";
    die(); }

identical:

if ($word1 !== $word2) {
    echo "Your words do not match, please go back and correct this.";
    die(); }

Th code runs fine with both of these but I would still like a detailed explanation as to when use which one, for future references, and to learn.

Thank you!

Ian V.
  • 13
  • 2
  • [See the manual](http://php.net/manual/en/language.operators.comparison.php) – RiggsFolly Dec 10 '15 at 09:47
  • Use the `!==` operator if you want to assure that both variables are the same type – Mihai Matei Dec 10 '15 at 09:47
  • 2
    @MateiMihai Use what _if you want to assure that both variables are the same type_ – RiggsFolly Dec 10 '15 at 09:48
  • Using the 3 character versions i.e. `===` or `!==` etc basically checks the equality or inequality but also tests the type of left and right hand side variables – RiggsFolly Dec 10 '15 at 09:50
  • @RiggsFolly It should be `!==` instead of `!===`, shouldn't it? – Parixit Dec 10 '15 at 09:51
  • 1
    Read about [PHP type juggling](http://php.net/manual/en/language.types.type-juggling.php) and check the [type comparison table](http://php.net/manual/en/types.comparisons.php) in the documentation. – axiac Dec 10 '15 at 10:24
  • http://stackoverflow.com/questions/1139154/is-there-a-difference-between-and-in-php | http://stackoverflow.com/questions/6356826/comparing-versus | http://stackoverflow.com/questions/3737139/reference-what-does-this-symbol-mean-in-php – Pang Dec 10 '15 at 10:25

2 Answers2

1

You can understand the difference between them by looking at Types comparison table in PHP manual.

Main difference is that !== is strict about type of compared values while != is weaker check.

Flying
  • 4,422
  • 2
  • 17
  • 25
0

the one will pass the other one will not the frist one cheks only for equal the second one checks and for type of var. The var $word1 is string the $word2 is a integer

if ($word1 != $word2) {
    echo "Your words do not match, please go back and correct this.";
    }
//with this if stament will pass the test without echo out nothing.

if ($word1 !== $word2) {
    echo "Your words do not match, please go back and correct this.";
     } //this one will not pass and will echo out your string 
 ?>