Operators in PHP

Hi Guys,
Today we discuss operators in php. The operators are in PHP helps to perform different functionalities in php. It is mainly used to perform operations in values and variables. An operator is one type symbol, that is used to perform certain operations on its operands. Operator is always associate with operand.




Example

x+y

Here x and y are the operand and ‘+’ is the operator

In PHP language supports following type of operators.

  1. Arithmetic Operators
  2. Logical Operators
  3. Conditional Operators
  4. Comparison Operators
  5. Assignment Operators

1) Arithmetic Operator




In php the arithmetic operator helps to perform common arithmetic operation like addition, multiplication, division etc. In other words, it performs mathematical operations on its operands.

Operator Purpose Example
+ Perform addition of two operands. $x=10; $y=2; $z=$x+$y;
$z=12
Perform substraction of two operands. $x=10; $y=2; $z=$x-$y;
$z=8
* Perform multiplication of two operands. $x=10; $y=2; $z=$x*$y;
$z=20
/ Perform divide left operand by the right operand $x=10; $y=2; $z=$x/$y;
$z=5
% Divide left operand by the right operand and return remainder of division $x=11; $y=2; $z=$x/$y;
$z=5
++ Increment Operator, increase the integer values by 1. $x=10; $x++ will give 11
Decrement Operator, decrease the integer values by 1. $x=10; $x– will give 9




2) Logical Operators

It helps to perform logical operations on its operands.

Operator Purpose Example
&& If both the operands are non zero then condition becomes true. $x=true;$y=false;$z=$x && $y;
$z=false;
|| If any of the two operands are non zero then condition becomes true. $x=true;$y=false;$z=$x || $y;
$z=true;
! If a condition is true then Logical NOT operator will make false. $x=true; !($z=$x);
$z=false;

3) Conditional or Ternary Operator

There is one more operator called conditional operator. This first evaluates an expression for a true or false value and then execute one of the two given statements depending upon the result of the evaluation.




Operator Purpose Example
? : It is used for conditional expression. If Condition is true ? Then value X : Otherwise value Y

4) Comparison Operator

It helps you to perform comparision betweeen two operands.

Operator Purpose Example
== Checks if the value of two operands are equal or not, if yes then condition becomes true. (x == y) is not true.
!= Checks if the value of two operands are equal or not, if values are not equal then condition becomes true. (x != y) is true.
> Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. (x > y) is not true.
< Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true. (x < y) is true
>= Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true. (x >= y) is not true.
<= Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true. (x <= y) is true

5) Assignment Operator

It allows you to assign value of one variable to another variable.




Operator Purpose Example
= Simple assignment operator, Assigns values from right side operands to left side operand z = x + y will assign value of x + y into z
+= Add AND assignment operator, It adds right operand to the left operand and assign the result to left operand z += x is equivalent to z = z + x
-= Subtract AND assignment operator, It subtracts right operand from the left operand and assign the result to left operand z -= x is equivalent to z = z – x
*= Multiply AND assignment operator, It multiplies right operand with the left operand and assign the result to left operand z *= x is equivalent to z = z * x
/= In here, it divides left operand with the right operand and assign the result to left operand z /= x is equivalent to z = z / x
%= Modulus AND assignment operator, It takes modulus using two operands and assign the result to left operand z %= x is equivalent to z = z % x

If anyone has doubts on this topic then please do let me know by leaving comments or send me an email.

5 thoughts on “Operators in PHP

  1. no depost

    On this platform, you can discover a great variety of casino slots from top providers.
    Visitors can experience traditional machines as well as new-generation slots with stunning graphics and interactive gameplay.
    Even if you’re new or a seasoned gamer, there’s a game that fits your style.
    play casino
    The games are instantly accessible anytime and optimized for desktop computers and mobile devices alike.
    No download is required, so you can start playing instantly.
    The interface is user-friendly, making it quick to explore new games.
    Join the fun, and enjoy the world of online slots!

  2. snaptik

    This is a topic that is near to my heart… Take care! Exactly where can I find the contact details for questions?

  3. bij nl

    Good day! Do you know if they make any plugins to assist
    with Search Engine Optimization? I’m trying to get
    my site to rank for some targeted keywords but I’m not seeing very good success.
    If you know of any please share. Thanks! You can read
    similar blog here: Wool product

  4. auto approve list

    Hi it’s me, I am also visiting this web site on a regular basis,
    this web site is really fastidious and the viewers are actually sharing
    fastidious thoughts.!

Leave a Reply

Your email address will not be published.