OPERATORS IN PHP


OPERATORS IN PHP

  • Arithmetic Operators.
  • Assignment Operators.
  • Comparison Operators.
  • Increment and decrement operators.
  • Logical Operators.

Arithmetic operator

They are used with numeric values to perform common arithmetical operators, such as addition, subtraction, multiplication e.t.c.

Operator                    Name                          example                      result

      +                            Addition                       $x+$y                           sum of x and y

      –                             subtraction                  $x-$y                           difference of x and y

      *                            Multiplication             $x*$y                           product of x and y

      /                             Division                       $x/$y                           Quotient of x and y

     %                            Modulus                      $x%$y                          Remainder of x and y

     **                           Exponential                 $x**$y Result of raising $ x to the $y’th power

     e.g

(+)

<?php

$x=10;

$y=2;

$z=$X+$y

Echo $z;

?>

// outputs:12

(_)

 <?php 

$x=10;

$y=2;

$z=$X-$y

Echo $z;

?>

 

// outputs:8

  Assignment Operators

They are used with numeric values to write a value to a variable.

The basic assignment operator in php is ”=”.It means the left operand get set to the value of the assignment expression on the right.

Assignment Same as description
X=y X=y The left operands gets sets to the value of the expression on the right
X+=y X=x+y Addition
x-=y X=x-y Subtraction
X*=y X=x*y Multiplication
x/=y X=x/y Division
X%=y X=x%y modulus

e.g

 <?php

$x=20;

Echo $x;

2)  (x+=y)

$x=40;

$y=20;

x+=y

echo $x;

?>

//0utputs:60

PHP COMPARISON OPERATORS

The php comparison operators are used to compare two values (numbers or strings ).

Operator name Example Result
== Equal $X==$y Result true if $x is equal to $y.
=== Identical $X===$y Result true if $x is equal to $y and they are of the same type.
!= Not equal $X!=$y Result true if $x is not equal to $y
<>  Not equal $X<>$y Result true if $x is not equal to $y
!== Not identical $X!==$y Result true if $x is not equal to $y and they are not of the same type.
Greater than $X>$y Returns true if $x is greater than $y
Less than $X<$y Returns true if $x is less than $y
>= Greater than or equal to $X>=$y Returns true if $x is greater than $y or equal to y
<= less than or equal to $X<=$y Returns true if $x is less than $y or equal to y

  e.g

 ?php 

$x=34;

$x=34;

?>

ech0 ($x==$y);      //outputs will be true

ech0 ($x===$y);     //outputs will be true

ech0 ($x!==$y);     //outputs will be false

PHP INCREMENT/ DECREMENT OPERATORS

Operators name description
++$x Pre-increment Increment $x by one , the returns $x
$x++ Post-increment Returns $x, then increment $x by one
–$x Pre-decrement decrement $x by one, then returns $x
$x– Post-decrement Returns $x, then decrement $x by one
 <?php

$x=10;

Echo ++$x;   //output will be 11 (increment by 1)

Echo $x;   //output will be 11 (return $x)

?>

2) $x++

$x=10;

Echo $x++ ;       //output will be 10 (return $x)

Echo $x;          //output will be 11 (increment by 1)

PHP LOGICAL OPERATORS

The php logical operators are used to combine conditional statements.

operators name example result
and And $X and $Y True if both $x and $y are true
Or Or $X or $Y True if either $x or $y is true
Xor Xor $X xor $Y True if either $x or $y is true, but not both
&& And $X && $Y True if both $x and $y are true
// Or $X //  $Y True if both $x and $y is true
! not !$x True if $x is not true

e.g

<?php

$coke=100;

If(($$coke =100) && ((&coke>=100))) {

Echo “buy it”;

}

Else{

Echo “don’t buy it”;

}

//output will be “buy it” cause both are true.