Php if strings not equal

PHP type comparison tables

The following tables demonstrate behaviors of PHP types and comparison operators, for both loose and strict comparisons. This supplemental is also related to the manual section on type juggling. Inspiration was provided by various user comments and by the work over at » BlueShoes.

Before utilizing these tables, it’s important to understand types and their meanings. For example, «42» is a string while 42 is an int . false is a bool while «false» is a string .

Note:

HTML Forms do not pass integers, floats, or booleans; they pass strings. To find out if a string is numeric, you may use is_numeric() .

Note:

Simply doing if ($x) while $x is undefined will generate an error of level E_NOTICE . Instead, consider using empty() or isset() and/or initialize your variables.

Note:

Some numeric operations can result in a value represented by the constant NAN . Any loose or strict comparisons of this value against any other value, including itself, but except true , will have a result of false . (i.e. NAN != NAN and NAN !== NAN ) Examples of operations that produce NAN include sqrt(-1) , asin(2) , and acosh(0) .

Comparisons of $x with PHP functions

Expression gettype() empty() is_null() isset() bool : if($x)
$x = «»; string true false true false
$x = null; NULL true true false false
var $x; NULL true true false false
$x is undefined NULL true true false false
$x = []; array true false true false
$x = [‘a’, ‘b’]; array false false true true
$x = false; bool true false true false
$x = true; bool false false true true
$x = 1; int false false true true
$x = 42; int false false true true
$x = 0; int true false true false
$x = -1; int false false true true
$x = «1»; string false false true true
$x = «0»; string true false true false
$x = «-1»; string false false true true
$x = «php»; string false false true true
$x = «true»; string false false true true
$x = «false»; string false false true true

Loose comparisons with ==

true false 1 0 -1 «1» «0» «-1» null [] «php» «»
true true false true false true true false true false false true false
false false true false true false false true false true true false true
1 true false true false false true false false false false false false
0 false true false true false false true false true false false * false *
-1 true false false false true false false true false false false false
«1» true false true false false true false false false false false false
«0» false true false true false false true false false false false false
«-1» true false false false true false false true false false false false
null false true false true false false false false true true false true
[] false true false false false false false false true true false false
«php» true false false false * false false false false false false true false
«» false true false false * false false false false true false false true

* true prior to PHP 8.0.0.

Strict comparisons with ===

true false 1 0 -1 «1» «0» «-1» null [] «php» «»
true true false false false false false false false false false false false
false false true false false false false false false false false false false
1 false false true false false false false false false false false false
0 false false false true false false false false false false false false
-1 false false false false true false false false false false false false
«1» false false false false false true false false false false false false
«0» false false false false false false true false false false false false
«-1» false false false false false false false true false false false false
null false false false false false false false false true false false false
[] false false false false false false false false false true false false
«php» false false false false false false false false false false true false
«» false false false false false false false false false false false true

Источник

PHP not equal (With Examples)

A PHP not equal is a comparison operator which is represented by a symbol ( != or <>).

This is always used if you want to compare a data type of two given (values) in any case of whether the two values were equal or not, that’s the time that we need to use the not equal operator.

In this article, we will discuss PHP not equal and why it is important to learn and understand. This article is a continuation of the previous topic, entitled Types of Operators.

What is PHP not equal?

In PHP, not equal is used to compare two given values of data types. This is a comparison operator represented by the symbol ( != or <>).

In addition, this not-equal operator returns true once the data type of the two given values is the same. Even if the value in the variables stored is not the same.

And this not-equal operator will return false once the data type of the two given values were not the same even though the value of the two variables stored are the same.

$variable1 != $variable2; $variable1 <> $variable2;

In order for us to know more about PHP equal I will provide an advanced example for you to use in your future development.

Comparison operators

The following programs below demonstrate a working program that uses an equal operator to compare a data type of the given two values and return the output.

The value stored in the first variable is: 10 The value stored in the second variable is: 10 The result returned after using not equal operator is: bool(false)
The value stored in the first variable is: Glenn Magada Azuelo The value stored in the second variable is: A writer and a computer programmer The result returned after using not equal operator is: bool(true)
The value stored in the first variable is: 26.97 The value stored in the second variable is: 16.664455 The result returned after using not equal operator is: bool(true)
The value stored in the first variable is: ITSOURCECODE.COM The value stored in the second variable is: SOURCECODEHERO.COM The result returned after using not equal operator is: bool(true)

How do I check if two strings are not equal in PHP?

In PHP, the strcmp() is a built-in function used to compare two strings.

In addition, this function is very case-sensitive, and the small and capital cases will be treated differently, during the comparison stage.

Conclusion

I hope this lesson has helped you learn a lot. Check out my previous and latest articles for more life-changing tutorials that could help you greatly.

Leave a Comment Cancel reply

You must be logged in to post a comment.

Источник

PHP Comparison Operators

Summary: in this tutorial, you will learn how to use PHP comparison operators to compare two values.

Introduction to PHP comparison operators

A comparison operator allows you to compare two values and returns true if the comparison is truthful and false otherwise.

The following table illustrates the comparison operators in PHP:

Operator Name Description
== Equal to Return true if both operands are equal; otherwise, it returns false .
!=, <> Not equal to Return true if both operands are equal; otherwise, it returns false .
=== Identical to Return true if both operands have the same data type and equal; otherwise, it returns false .
!== Not identical to Return true if both operands are not equal or not have the same data type; otherwise, it returns false .
> Greater than Return true if the operand on the left is greater than the operand on the right; otherwise, it returns false .
>= Greater than or equal to Return true if the operand on the left is greater than or equal to the operand on the right; otherwise, it returns false .
Less than Return true if the operand on the left is less than the operand on the right; otherwise, it returns false .
Less than or equal to Return true if the operand on the left is less than or equal to the operand on the right; otherwise, it returns false .

Equality Operator (==)

The equality returns true if both values are equal; otherwise, it returns false . The following example returns true because 10 is equal 10:

 $x = 10; $y = 10; var_dump($x == $y); // bool(true)Code language: HTML, XML (xml)

The following example returns false because 10 is not equal 20 :

  $x = 20; $y = 10; var_dump($x == $y); // bool(false)Code language: HTML, XML (xml)

The following example compares the number 20 with a string ’20’ , it also returns true .

 $x = '20'; $y = 20; var_dump($x == $y); // bool(true)Code language: HTML, XML (xml)

If you want to compare two values with the consideration of type, you can use the identical operator ( === ).

Not equal to operator (!=, <>)

The not equal to (!=, <>) operator returns true if the lefthand value is not equal to the righthand value; otherwise, it returns false . For example:

 $x = 20; $y = 10; var_dump($x != $y); // bool(true)Code language: HTML, XML (xml)

Identical operator (===)

The identical operator returns true if both values are equal and have the same type; otherwise returns false .

The following example uses the identical operator to compare a string and a number. It returns false because these values have different types:

 $x = '20'; $y = 20; var_dump($x === $y); // bool(false)Code language: HTML, XML (xml)

Not identical operator (!==)

The not identical operator (!==) returns true if the values are not equal or they do not have the same type; otherwise, it return false . For example:

 $x = 20; $y = 10; var_dump($x != $y); // bool(true) $x = 20; $y = '20'; var_dump($x != $y); // bool(false)Code language: HTML, XML (xml)

Greater than (>)

The greater than return true if the lefthand value is greater than the righthand value; otherwise, it returns false :

 $x = 10; $y = 20; var_dump($x > $y); // bool(false) var_dump($y > $x); // bool(true)Code language: HTML, XML (xml)

Greater than or equal to (>=)

The greater than or equal to operator returns true if the lefthand value is greater than or equal to the righthand value; otherwise, it returns false. For example:

 $x = 20; $y = 20; var_dump($x >= $y); // bool(true) var_dump($y >= $x); // bool(true)Code language: HTML, XML (xml)

Less than (<)

The less than operator returns true if the lefthand value is less than the righthand value; otherwise, it returns false. For example:

 $x = 20; $y = 10; var_dump($x < $y); // bool(false) var_dump($y < $x); // bool(true)Code language: HTML, XML (xml)

Less than or equal to (<=)

If the lefthand value is less than or equal to the righthand value, the less than or equal to operator returns true; otherwise, it returns false. For example:

 $x = 20; $y = 20; var_dump($x // bool(true) var_dump($y // bool(true)Code language: HTML, XML (xml)

In this tutorial, you have learned how to use the PHP comparison operators to compare two values of the same or different types.

Источник

How to check if strings are equal in PHP?

In this tutorial, you shall learn how to check if given two strings are equal in PHP using Equal-to operator, with example programs.

PHP – Check if strings are equal

To check if two given strings are equal in PHP, use Equal-to operator and pass the two strings as operands.

If both the strings are equal in value, then Equal-to operator returns a boolean value of true , else it returns false .

The boolean expression to check if two strings $x and $y are equal is

This expression can be used in a conditional statement like if, if-else, etc.

Examples

1. Positive Scenario (Equal strings)

In the following example, we take string values: $x and $y , and check if they are equal.

We have take the string values such that they are of equal value. Therefore, based on the program and given values, if-block must run.

PHP Program

PHP - Check if strings are equal

2. Negative Scenario (Not equal strings)

In the following example, we take different string values in $x and $y, and check if they are equal.

Since the string values are not equal in value, else-block must run.

PHP Program

PHP - Check if strings are equal

Conclusion

In this PHP Tutorial, we learned how to check if two given strings are equal or not, with the help of example programs.

Источник

Читайте также:  Php сохранение данных файл
Оцените статью