Comparison Operators

This operator is used to compare two given values.

Name Symbol Usage Description
Equal to == $a==$b The value of $a is equal to the value of $b.
Not equal to != $a != $b The value of $a is not equal to the value of $b.
Less than < $a <$b The value of $a is less than the value of $b.
Greater than > $a > $b The value of $a is more than the value of $b.
Less than equal to <= $a <=$b The value of $a is either less than or equal to the value of $b.
Greater than equal to  >= $a >=$b The value of $a is either more than or equal to the value of $b.
Identical === $a===$b The value of $a is equal to the value of $b when the data type of both is same.

The if construct

In most cases , you will need to use control structures, or constructs in your code to use the operator effectively. Control structures are statement that check conditions assign a value create a loop within the code or call a function.
The if construct is a conditional construct. It is use to check for a conditional statement and perform some action if the condition is true or false.
The if construct is fallowed by a logical expression in which data is compared and a decision is made best of the result of the comparison.

<?php
$a = 5;
$b = 7;
$c = $a+$b;
  if ($c > 9){ 
echo "The sum is greater than 9."; } ?>

The output of the above code will be: The sum is greater than 9.

The if construct has been used to check for a condition. Only when the sum of the variables $a and $b is more than 9 will the message "The sum is greater than 9" will appear. If the condition specified is false then no message will be displayed. Notice that the action to be performed is enclosed within curly brackets.

The else statement

The else statement is use to specified an action to be performed after a specific condition has been checked, and it is used with he if construct.
A condition can be either true or false. Therefore you might want to perform an action in both cases. The if and else construct can be used together to achieve this result. The fallowing is an example where if and else construct have been used:

<?php
$a = 10;
$b = 5;
$c = $a + $b;
if($c == 20) {
echo "The sum is equal to 20."; }

The output of the above code will be: The sum is not equal to 20.

Incrementing and Decrementing Operators

There are two types of incrementing and decrementing operators, post incrementing/decrementing operators and pre incrementing /decrementing operators. Post incrementing operators are placed after a variable name, and pre incrementing /decrementing operators are placed before the name.

Operator Usage Output
Post incrementing $a++; Returns the value of $a and then adds 1 to the value
Post decrementing $a--; (2 minus signs) Returns the value of $a and then subtract 1 from the value
Pre incrementing ++$a; Adds 1 to the value of $a and then returns the new value
Pre decrementing --$a; (2 minus signs) Subtract 1 from the value of $a and returns the new value


Consider the fallowing code for a better understanding of incrementing and decrementing operators. This code shows how the post incrementing operators works.

Post Incrementing Operator

<?php
$a = 8;
$b = $a++;
echo $b; //Returns the value 8.
echo $a; //Returns the value 9.
?>

Post Decrementing Operator

<?php
$a = 10;
$b = $a--;
echo $b; // Returns the value 5.
echo $a; // Returns the value 4.
?>

Pre Incrementing Operator

<?php
$a = 8;
$b = ++$a;
echo $b; // Returns the value 9.
echo $a; // Returns the value 9.
?>

Pre Decrementing Operator

<?php
$a = 10;
$b = --$a;
echo $b; // Returns the value 9.
echo $a; // returns the value 9.
?>

String Operator

When you start writing PHP code, you will realize the importance of string operators. PHP uses two kinds of string operators, concatenation (.) the dot sign and concatenating assignment (.=) the dot and equal to sign.The concatenation operator is used when you want to combine the left and right argument between which the operator is placed. The concatenating assignment operator is a combination of the concatenation operators and the assignment operator.

Concatenation Operator

<?php
$a = "My name is "; $b .= "Rishi";
echo $b;

The output of the code is: My name is Rishi.

Concatenating Assignment operator

<?php
$a = "My name is";
$a.= "Rishi"; echo $a;
?>

The output of the code is: My name is Rishi.

Logical Operators

The Logical operators are used in PHP to determine the status of condition . When you use if...else or while statements, logical operator execute code based on the status of a condition .In this section, we will discuss only two logical operators:

The and operate (&&) is used when a statement has two condition to be checked. If both the condition are true , the statement is true,.

The or operator ( || ) is also used when a statement has two condition is true, the statement is true.

<?php
  $a = 10;
  $b = 15;
  $c = $a + $b;
  if( ($a > 5) && $c < 20 ){
    echo "Value of c is less than 20";
  }else {
    echo "Value of c is more than 20"; 
  }
?>


The Output of the above code is: Value of c is more than 20



Top