PHP Lesson 10 – Comparison operators

Hello viewer, You can now have our Tutorial Lessons in your android mobile device and read it offline.
Download kotlin Programming APP on PlayStore
Download Website SEO Lessons APP on PlayStore

PHP Lesson 10 - Comparison operators - PHP Lesson 10 - Comparison operators - PHP Lesson 10 - Comparison operators - Comparison operators
Comparison operators

Comparison operators in PHP allow you to compare values and variables. For example, they can be used to check whether a variable has a specific value, for example.

operatorNameexplanation
$== $bEqualThis comparison is true if $a and $b contain the same value. If the types of variables are different, they will be converted.
$=== $bIdenticalThis comparison is true if $a and $b have the same type and content. If one value was of type int and the other was of type String, then false would be returned.
$!= $bunequalThis comparison is true if $a and $b do not contain the same value. If the types of variables are different, they will be converted.
$!== $bIdenticalThis comparison is true if $a and $b are of a different type or value.
$$bSmaller$a must be less than $b.
$<= $bLittle match$a must be less than or equal to $b.
$$bGreater$a must be greater than $b.
$>= $bGreater or equal$a must be greater than or equal to $b.

Comparison of variables and values – PHP Programming

Using the above operators you can compare variables as well as values in PHP. The result of the comparison is always either true or false. The result of the comparison can either be stored in a variable or used directly in the condition of if statement or loops.

Following is a short example of the comparison operators. To output the value of the comparison, the function var_dump ($variable) is used. It is not possible to output a boolean value via echo.

<?php
$int1 = 15;
$int2 = 20;

if ($int1 <$int2) { // Comparison of two variables
  echo "$Int1 is less than $int2 <br />";
}

if ($int1 <= 100) { // comparison of variable and value
  echo "$int1 is less than or equal to 100 <br />";
}

// storing the comparison in a variable
$animal = "cat";
$string_comparison = ($animal == "cat");

echo 'The value of the variable $string_comparison is:';
var_dump ($string_comparison); // Output what value the variable has

if ($string_comparison) {
   echo "--- The string comparison had the true value -";
}
?>

Difference between == and === in PHP with example

In PHP the comparison operators == and === exist to check for equality and != And !== to check for inequality.

The difference between == and === , that with == only the value is checked, with === additionally the type of the variable is checked. Have you in the deposited a variable such as the integer 1, the other variable the string “1”, so would == a true return, the operator === but false, because the types of the variables were different.

The following code shows you the differences:

<?php
$integer = 1;
$string = "1";

echo "Result of ==:";
var_dump ($integer == $string);

echo "<br /> Result from ===:";
var_dump ($integer === $string);
?>

The situation is similar with != And !== . The comparison operator != Checks if the contents are different. With !== the types of variables are checked first. If these are different, then true is returned, even if the content is the same:

<?php
$integer = 1;
$string = "1";

echo "Result of =:";
var_dump ($integer != $string);

echo "<br /> Result from ==:";
var_dump ($integer !== $string);
?>

In the above example $integer != $String results in false because the content is not different. On the other hand, $integer !== $string results in true, because the types of variables were different (string and integer).

 

php === operator vs == When do I use?

In most cases, ==   or !=   Is used, but it is necessary to   switch to ===   or !== in some situations . The function strpos($text, $searchword) returns the position of the $searchword in $text. If it is not found, false is returned. Now strpos() can also return position 0 if the search term is at the beginning of the text. However, as the following example shows, a comparison with != Would not work here:

<?php
$text = "This is a text";
$searchword = "this";

$position = strpos ($text, $searchword);

if ($position == false) {
   echo "Your search term was not found in the text";
} else {
   echo "Your search word was found at position $position";
}
?>

The above script erroneously states that the search term was not found. This is because PHP also takes the value 0 as false. The correct use is by === :

<?php
$text = "This is a text";
$searchword = "this";

$position = strpos ($text, $searchword);

if ($position === false) {
   echo "Your search term was not found in the text";
} else {
   echo "Your search word was found at position $position";
}
?>

The if statement is only true if strpos() actually returned false.

About The Author

Related posts

Leave a Reply