Not Equal to != and !== in PHP

Not equal to != and !== in PHP

== and != do not take into account the data type of the variables you compare. So these would all return true:

'0'   == 0
false == 0
NULL == false

=== and !== do take into account the data type. That means comparing a string to a boolean will never be true because they're of different types for example. These will all return false:

'0'   === 0
false === 0
NULL === false

You should compare data types for functions that return values that could possibly be of ambiguous truthy/falsy value. A well-known example is strpos():

// This returns 0 because F exists as the first character, but as my above example,
// 0 could mean false, so using == or != would return an incorrect result
var_dump(strpos('Foo', 'F') != false); // bool(false)
var_dump(strpos('Foo', 'F') !== false); // bool(true), it exists so false isn't returned

Difference between not equal operators and != in PHP

In the main Zend implementation there is not any difference. You can get it from the Flex description of the PHP language scanner:

<ST_IN_SCRIPTING>"!="|"<>" {
return T_IS_NOT_EQUAL;
}

Where T_IS_NOT_EQUAL is the generated token. So the Bison parser does not distinguish between <> and != tokens and treats them equally:

%nonassoc T_IS_EQUAL T_IS_NOT_EQUAL T_IS_IDENTICAL T_IS_NOT_IDENTICAL
%nonassoc '<' T_IS_SMALLER_OR_EQUAL '>' T_IS_GREATER_OR_EQUAL

php is not equal to and is not equal,equal to

== and != check equality by value, and in PHP you can compare different types in which certain values are said to be equivalent.

For example, "" == 0 evaluates to true, even though one is a string and the other an integer.

=== and !== check the type as well as the value.

So, "" === 0 will evaluate to false.


Edit: To add another example of how this "type-juggling" may catch you out, try this:

var_dump("123abc" == 123);

Gives bool(true)!

PHP: How does the Not-equal and Not-identical Operators work and are they faster then the Equal or Identical Operators?

Strings are defined as a structure here:

typedef struct {
char *c;
size_t len;
size_t a;
} smart_string;

The equality operator is defined here. (The following three equality operators also perform in essentially the same way, except they skip the address check as it would always be false)

static zend_always_inline zend_bool zend_string_equals(zend_string *s1, zend_string *s2)
{
return s1 == s2 || (ZSTR_LEN(s1) == ZSTR_LEN(s2) && !memcmp(ZSTR_VAL(s1), ZSTR_VAL(s2), ZSTR_LEN(s1)));
}

In case you don't speak C:

First, the address of each string structure is compared, if these are equal then the strings must be equal. Otherwise, further checks are made.

Second, if the addresses are not equal, then the length of each string is compared. This is just an integer equality check as the length is part of the string's structure definition. If the lengths are not equal, false is returned.

Next, the memory contents are checked for each string with memcmp. As memcmp returns 0 if the memory contents are equal, this is negated to return true.

To explicitly answer your question: PHP avoids checking every character of a string, the only case in which every character would be checked is that if every character of the string except for the last character is equal, and that the lengths of the strings are the same.

I must say: If you are really worrying about === being slower than !==, then you really shouldn't be.

PHP if not equal(!=) and or (||) issue. Why doesnt this work?

I am not exactly sure what you want, but that logic will always evaluate to true. You might want to use AND (&&), instead of OR (||)

The furthest statement that is ever tested is ($str != "danielle") and there are only two possible outcomes as PHP enters the block as soon as a statement yields true.

This is the first:

$str = "dan";

$str != "joe" # true - enter block
$str != "danielle" #ignored
$str != "heather" #ignored
$str != "laurie" #ignored
$str != "dan" #ignored

This is the second:

$str = "joe";

$str != "joe" # false - continue evaluating
$str != "danielle" # true - enter block
$str != "heather" #ignored
$str != "laurie" #ignored
$str != "dan" #ignored

If the OR was changed to AND then it keeps evaluating until a false is returned:

$str = "dan";

$str != "joe" # true - keep evaluating
$str != "danielle" # true - keep evaluating
$str != "heather" # true - keep evaluating
$str != "laurie" # true - keep evaluating
$str != "dan" # false - do not enter block

The solution doesn't scale well though, you should keep an array of the exclude list and check against that do:

$str = "dan";
$exclude_list = array("joe","danielle","heather","laurie","dan")
if(!in_array($str, $exclude_list)){
echo " <a href='/about/".$str.".php'>Get to know ".get_the_author_meta('first_name')." →</a>";
}

Is there a difference between !== and != in PHP?

The != operator compares value, while the !== operator compares type as well.

That means this:

var_dump(5!="5"); // bool(false)
var_dump(5!=="5"); // bool(true), because "5" and 5 are of different types

Simpler way to check if variable is not equal to multiple string values?

For your first code, you can use a short alteration of the answer given by
@ShankarDamodaran using in_array():

if ( !in_array($some_variable, array('uk','in'), true ) ) {

or even shorter with [] notation available since php 5.4 as pointed out by @Forty in the comments

if ( !in_array($some_variable, ['uk','in'], true ) ) {

is the same as:

if ( $some_variable !== 'uk' && $some_variable !== 'in' ) {

... but shorter. Especially if you compare more than just 'uk' and 'in'.
I do not use an additional variable (Shankar used $os) but instead define the array in the if statement. Some might find that dirty, i find it quick and neat :D

The problem with your second code is that it can easily be exchanged with just TRUE since:

if (true) {

equals

if ( $some_variable !== 'uk' || $some_variable !== 'in' ) {

You are asking if the value of a string is not A or Not B. If it is A, it is definitely not also B and if it is B it is definitely not A. And if it is C or literally anything else, it is also not A and not B. So that statement always (not taking into account schrödingers law here) returns true.

PHP: Multiple conditions with NOT Equal (!=) operator is not working

Better code:

$allowed = array('jpeg','png');

if(in_array($ext,$allowed)){
echo "Correct";
}
else {
echo "Wrong";
}

Why does PHP have two not equal to operators (!= and )

I guess the <> operator has been implemented in PHP because it is present in some other languages (SQL, for instance)

Both <> and != mean almost exactly the same : the only difference I've managed to find is related to their precedence : see Operator Precedence : they are not on the same line ^^ (which means there's a difference, afterall, between those two)

Though, I have to admit I have never seen the <> used in PHP.

Note that people generally use != ; you should probably do the same : it will make your code easier to understand.

And, btw, you also have the !== operator, which does type comparison too ; but there is no <<>> operator or anything like that ^^


As a reference : Comparison Operators

Not Equal to in php not working

try this with && instead of || in if

Onlie Demo

$food="7:1,address:mandakini Pune,clusterid:1,email:s@gmail.com,mobile:950389,name:san,valid_order_id:FD1445853852";
$items = explode ( ",", $food );
for($i=0;$i < sizeof($items);$i++){
$jsonval= $items[$i];

$inneritems = explode ( ":", $jsonval );
$item1=$inneritems[0];
$item2=$inneritems[1];

// echo '<br/>items '.$item1.":".$item2.'<br/>';
if($item1 !="clusterid" && $item1!="name" && $item1!="email" && $item1!="mobile" && $item1!="address" && $item1!="valid_order_id"){
echo '<br/>items '.$item1.":".$item2.'<br/>';
echo 'hello '.$item1.":".$item2;
}
}


Related Topics



Leave a reply



Submit