Web Development Tutorials




  
  

Operators

PHP Operators

Like other programming languages, PHP has operators that are used to perform operations or actions on variables. Operations include assigning a value to a variable, performing arithmetic operations with variables, comparing the values of variables, and determining the status of a condition.

  • Arithmetic Operators
  • Assignment Operators
  • Comparison Operators
  • Logical Operators

Arithmetic operators are used to perform basic mathematical operations. The following are arithmetic operators used in PHP:

Operator	Description
+	Performs addition
-	Performs subtraction
*	Performs multiplication
/	Performs division
%	Performs modulus (division remainder)
++	Increments value
--	Decrements value
<!DOCTYPE html>
<html>
<head>
  <title>A Web Page</title>
</head>
<body>
 <p>
  <?php
  
       // The addition operator
       $sum = 5 + 2;  //$sum = 7;
       $newsum = $sum + 4;

       echo "The sum is " . $newsum;

       //The subtraction operator
       $difference = $newsum - 2;

       echo "The difference is " . $difference;

       //The multiplication operator
       $product = $difference * 3;

       echo "The product is " . $product;

       //The division operator
       $quotient = $product / $difference;

       echo "The quotient is " . $quotient;

       //The Increment operator
       $quotient++;

       echo $quotient;

       //The decrement operator
       $quotient--;

       echo $quotient;

  ?>
 </p>
</body>
</html>
   The sum is 11 
   The difference is 9 
   The product is 27 
   The quotient is 3 
   4 
   3 
   

Order of Operations

When an arithmetic expression is evaluated, there is a given order in which the operations are carried out. This order is called operator precedence. Multiplication and division take precedence (are done first) over addition and subtraction, moving from left to right through the expression. This order has important impact on whether or not you get the expected results. Consider the following declarations and assignments.

  <?php 
  
    $num1 = 4;
    $num2 = 5;
    $num3 = 2;

    $answer = $num1 * $num2 - $num3;

    echo $answer;
 
  ?>

The resulting value would be 18. First, $num1 is multiplied by $num2 to get 20; then $num3 is subtracted from 20 to get 18. Suppose, however, that you really intended to first subtract $num3 from $num2 and then multiply by $num1 to get 12. The above expression will not produce the intended result because multiplication takes precedence over subtraction and will be performed first.

Often, therefore, you need to override operator precedence and explicitly control the order of evaluation of an expression. This is done by using parentheses ( ) to govern the sequence in which arithmetic operations are carried out. To get your intended result, you would rewrite the statements.

  <?php 
  
    $num1 = 4;
    $num2 = 5;
    $num3 = 2;

    $answer = $num1 * ($num2 - $num3);

    echo $answer;
 
  ?>
  

In the above example code, the value of $answer will be 12. $num3 is subtracted from $num2 to get 3. Next 3 is multiplied by $num1.

Assignment operators are used to change the value of the current variable with the value on the right of the operator. The following are assignment operators used in PHP:

Operator	Description
=	Assigns the value of the variable on the right to the value of the variable on the left
+=	Adds the value on the left to the value on the right and assigns the result to the variable on the left.
-=	Subtracts the value on the left to the value on the right and assigns the result to the variable on the left.
*=	Multiplies the value on the left to the value on the right and assigns the result to the variable on the left.
/=	Divides the value on the left to the value on the right and assigns the result to the variable on the left.
%=	Divides the value on the left to the value on the right and assigns the remainder (modulus) to the variable on the left.
.=	The value on the left is added on to the value on the right and is assigned to the variable on the left.

Comparison operators are used to compare the value of variables. The following are comparison operators used in PHP:

Operator	Description
==	is equal to
!=	is not equal to
>	is greater than
<	is less than
>=	is greater than or equal to
<=	is less than or equal to

Logical operators allow you to determine the status of conditions. Depending on the condition of a variable different script actions can occur. Logical operators are used extensively with PHP decision structures. The following are Logical operators used in PHP:

Operator	Description
&&	AND
||	OR
!	NOT

TOP | NEXT: Strings