PHP Operators

PHP Operators

PHP Operators

Before moving on to learn what PHP can do for you, take a moment to learn about the various operators it provides.

Operators are the symbols that are used to perform certain operations on data.Different types of operators are used in  computer programming languages.
Example:
3 + 4 is equal to 7. In this expression 3 and 4 are operands and + symbol is called Operator.

PHP provides a variety of operators. List of PHP Operators is as follows:

  • Arithmetic Operators
  • Increment and Decrement Operators
  • Assignment Operators
  • Logical Operators
  • Comparison Operators

Arithmetic Operators

The symbols that performs mathematical operations such as addition, subtraction, and etc on data is called Arithmetic Operators. All arithmetic operators work with numeric values. Lets suppose we have two variables j and k where j = 10 and k = 5.

Operator    Operation         Description                                                            Example             Result

+                Addition              Adds two values                                                      $j + $k                15
−                Subtraction        Subtract one value from another                              $j − $k                5
*                 Multiplication     Multiple two values                                                   $j * $k                 50
/                 Division              Divides one value by another                                  $j  / $k                 2
%               Modulus            Gives the remainder of division of two integers        $j % $k               0

Increment and Decrement Operators

The increment operator is used to increase the value of a variable  by 1 and denoted by the symbol ++. The increment operator cannot increment the value of constants and expressions. For example, J++ and K++ are valid statements but 11++ is an invalid statement.

The decrement operator is used to decrease the value of a variable  by 1. It is denoted by the symbol –. The decrement operator cannot decrement the value of constants and expressions.For example, J– and K– are valid statements but 11– is an invalid statement.

Both increment and decrement operators can be used in two forms in PHP:

  • Prefix Form
  • Postfix Form

Lets suppose, we have a variable j where j =3

Operator       Operation                      Description                                                          Example             Result
++$j               Prefix Increment              Increments value by 1                                         ++$j                      4
–$j                 Prefix Decrement            Decrements value by 1                                        –$j                       2
$j++               Postfix Increment            Returns value, then Increments value by 1         $j++                      4
$j–                 Postfix Decrement          Returns value, then Decrements value by 1        $j–                       2

 

Assignment Operators

The PHP Operators that are used to assign values to variables called Assignment Operators. The assignment operator = is used in assignment statement to assign a value or computational result to a variable. The start is very simple from = symbol and move on to +=, −=, and so on.
Let suppose $j = 15 and $k = 4.

Operator       Example          Equivalent to         Result
=                    $j = 15             $j = 15                         15
+=                  $j += 5             $j = $j + 5                    20
−=                  $j −= 3             $j = $j − 2                    13
*=                   $j *= 8             $j = $j * 8                     120
/=                   $j /= 16            $j = $j / 3                      5
.=                   $j .= $k            $j = $j . $k                    60
%=                $j %= 4            $j = $j % 4                    0.6

 

Logical Operators

logical operator is a symbol or word used to combine conditional expressions or statements. As a rule, if something has a TRUE or FALSE value, it can be input to a logical operator.

Operator             Description                          Example
&&                        And                                         $j == 3 && $k == 2
and                       Low-precedence and              $j == 3 and $k == 2
||                           Or                                            $j < 5 || $j > 10
or                          Low-precedence or                 $j < 5 or $j > 10
!                            Not                                           ! ($j == $k)
xor                        Exclusive or                            $j xor $k

 

Comparison Operators

Leave a Reply

Your email address will not be published.