php

What is the difference between == and === in PHP

In this tutorial, we’re going to see two ways to compare variables for equality in PHP.
 

The general behavior of “==”

In most programming languages, the comparison operator (==) checks, on the one hand, the data type and on the other hand the content of the variable for equality. The standard comparison operator (==) in PHP behaves differently. This tries to convert both variables into the same data type before the comparison and only then checks whether the content of these variables is the same. The following results are obtained:

<?php
    var_dump( 1 == 1 ); // true
    var_dump( 1 == '1' ); // true
    var_dump( 1 == 2 ); // false
    var_dump( 1 == '2' ); // false
    var_dump( 1 == true ); // true
    var_dump( 1 == false ); // false
?>

Output:

bool(true)
bool(true)
bool(false)
bool(false)
bool(true)
bool(false)
 
This behavior should always be kept in mind when using “==”. In particular, it should be noted that (int) 0, (bool) false, (string) ‘ ‘, (string) ‘0’ and NULL are all considered the same (but not (float) 0.0):

<?php
    var_dump( 0 == false ); // true
    var_dump( 0 == '' );    // true
    var_dump( 0 == '0' );   // true
    var_dump( 0 == null );  // true
?>

Output:

bool(true)
bool(true)
bool(true)
bool(true)
PHP MCQ - Multiple Choice Questions and AnswersPHP Questions and Answers – Object-Oriented OOP – Part 1This collection of PHP Multiple Choice Questions and Answers (MCQs): Quizzes & Practice Tests with Answer focuses on “Object-Oriented OOP”.   1. The concept of…Read More
Behavior of “==” using the example of strpos

This is important for functions which can return an integer value from (int)0 to (int)n as well as the value (bool)false. strpos($str, $needle) is an example. If the $needle string is found in $str, strpos returns its position, which can also be (int)0 (the string is then right at the beginning of the $str string). If $needle is not found, (bool)false is returned. The following code is therefore false:

<?php
    $str = 'Stack How To';
    $needle = 'Stack';
    // would be true if we apply the identity operator === 
    if (strpos($str, $needle) == false) {
        echo('Not found!');
    } else {
        echo('Found');
    }
?>

Output:

Not found!
 

Behavior of “===” using the example of strpos

If you want to avoid these problems, then you need to use the operator “===” (the negation “!==”). This operator also checks the datatype of the variable and returns (bool)true only if both variables have the same content and the same datatype. The following would therefore be correct:

<?php
    $str = 'Stack How To';
    $needle = 'Stack';
    // would be true if we apply the identity operator === 
    if (strpos($str, $needle) === false) {
        echo('Not found!');
    } else {
        echo('Found');
    }
?>

Output:

Found

If possible, “===” (or “!==”) should always be used – and not “==” (or “!=”), because the first cannot lead to unexpected behavior by ignoring the data types.
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *