How to Work Operators in PHP 2024

Operators are used in programming and scripting language to do some arithmetic programming language and also it used in same manner. Operators perform operation on calculations also used for some logical functionality. In PHP operators are same as other variable and values.

In any kind of operation there are two things are used operator and operand. For example 5+5 in this statement 5 is consider as a operand and + is consider as an operator. Operators are some special characters that are used from a different perspective for different meninges in program logic. Operators are divided into two different categories.

Unary prefix operators, which precede a single operand.

Binary operators, which take two operands and perform a variety of arithmetic and Logical operation.

Type of Operators –

  • Arithmetic
  • Comparison
  • Logical
  • Assignment
  • Conditional
  • Increment/Decrement
  • String

1. Arithmetic Operators –

Arithmetic Operator is used to do some arithmetic Operations in program like addition, subtraction, multiplication, division. modulation. This operator is belonging to binary operator where two operands and one operator are used. Used for Addition/Sum of two number or two variable.

OperatorDescriptionExample
+Used for Addition/Sum of two number or two
variables
10+-90:
Sa+Sb;
Used for Subtraction of two number or two variables.100-50;
Sa-Sb;
*Used for Multiplication of two number or two
variables
50*50;
Sa*Sb
/Used for Division of two number or two variables50/2;
Sa/Sb
%Used for get Reminder of two number or two50%4;
Sa%Sb:
Arithmetic Operators Table
Example of Arithmetic Operators :

<?php

Sa=40;

Ssum-5+10;

Sb-60;

Ssubtraction=Sa-Sb;s

Smultiply=5*Sb;

Sdivision=Sb/3;

Smodulo-Sa%3;

echo(“Sum-Ssum <br>”):

echo(Subtraction=Ssubtraction <br>):

echo(“Multiplication=Smultiply <br>”);

echo(“Division=Sdivision <br>”);

echo(“Modulation=-Smodulo <br>”);

?>

How to Define Operators in PHP  2024
Operators

2. Comparison Operator –

This Operator is used to compare two operands and return logical value based on comparison. It return Boolean value based on Comparison (True/False). We can use any numerical or string operand to compare. Following are the Comparison Operators. It is also belonging to binary operator where two operands and one operator are used.

Operator DescriptionExample
Double
Equals (==)
Used for compare two operand. Returns true if
both are equal. This operator ignore the data
type of variable and value at the time of
Comparison
$a-3:$b=3
$a==$b: true
$a-3:$b=4
Not Equals (!=)
Not Equals (<>)
Used for compare two operands. Returns true
if both are not equal.
$a=3;$b=4;
$a!=$b: true
$a<>$b; true
Equals (===)Used for compare two operands. Returns true
if both are equal. This operator compares data
type of variable and value at the time of
Comparison, if both are same then it will
return true.
$a=3;$b=4;
$a===$b: true
$a===”5″; false
Greater than (>)This operator is used to compare two operands.
It will return true if left operand vale is greater
than right operand value;
$a=3;$b=4;
$a>$b: true
Less than (<) This operator is used to compare two operands.
It will return true if left operand vale is less
than right operand value;
$a=3;$b=4;
$a<$b: true;
Greater or Equals (>=)This Operator is used to compare greater and
equals both at same time. It will return true if
left operand value is either greater than or
equals to the right operand value
$a=3;
$b=4;
$a>=$b: true;
$a>=10; true;
Less or Equals (<=)This Operator is used to conpare less and
equals both at same time. It will return true if
left operand value is either less than or equals
to the right operand value.
$a=3;
$b=4;
$a<=$b: true;
$a<=10; true;
Comparison Operator Table

Example of Comparison Operator :

<?php

Sa-20;

Sb-10:

if(Sa>-Sb)

{

echo(“A is greater or Equal to B'”);

}

else if(Sa<-Sb)

{

echo(“B is Greater or Equal to A”);

}

else if(Sa==$b)

{

echo(“A and B are same and their Data types are also same'”):

}

else if(SaSb)

{

echo(“A and B are same but their Data types are not same”):

}

echo(“A and B are not same'”);

?>

3. Logical Operator –

Logical Operator is used to check logical value. They return Boolean value. It is used to compare two different Logic or Conditions.

Operator DescriptionExample
Logical And (&)This operator returns true if both expressions are fulfil
it’s condition.
$a>$b &&
$a>$c;
Logical or (||)This operator returns true if either first or second one of the expression is fulfil it’s condition.$a>$b ||
$a>$c;
Logical Not (!)This operator is reversing the Boolean result of the
Operand or Condition
!($a>$b)
return true
$a>$c;
Logical Operator Table

Example of Logical Operator :

<?php

Sa-‘; Sb=’;

if(Sa>100 | Sa<-200 && Sa!-0)

echo (“A is between 100 to 200′”):

if(Sb>=50 && Sb<=100)

echo (“Sb is between 50 to 100”);

if(:Sa && !Sb)

echo (“A and B is Empty”);

?>

4. Assignment Operator –

This operator is used to assign value to a variable. It will always assign new value in let side of operator variable. We can also use this operator to assign some computed Expression result value to the variable.

Operator DescriptionExample
Equalsto(=)This operator is used to assign value to a variable. It will always assign new value in let
side of operator variable. We can also use this operator to assign some computed
Expression result value to the left side operand.
a=3;b=3
a=b+10;
Plus and Equals to (+=)This operator is first of all Plus value of left side a=5;b=; Equals to(+=and right side operand and then assign result to left a= a+d;a=3;b=3
a+=b; now a=6
Minus and Equals to (*=)This operator is first of all Minus value of left side a=5;b=; Equals to(-=and right side operand and then assign result to left a-D,a=3;b=3
a-=b; now a=0
Minus and Equals to (*=)This operator is first of all Multiply value of left side and right side operand and then assign result to left side operand. It is equivalent of a=ab;a=3;b=3
a*=b; now a=9
Minus and Equals to (%=)This operator is first of all Modulo value of lef side a-5;b=5; and right side operand and then assign result to left side operand. It is equivalent of a=a*b;a=3;b=3
a%=b; now a=1
Minus and Equals to (/=)This operator is first of all Divide value of left side and right side operand and then assign result to left side operand. It is equivalent of a=a*b;a=3;b=3
a/=b; now a=1
Assignment Operator table

Example of Assignment Operator :

<?php

Sa=20; Sb=10; Sc=0;

Sct=Sa;

echo(“‘sum=”.Sc.”<br>);

Sc-=Sb:

echo(“Subtraction=”.Sc.”<br>”);

Sb*=Sa;

echo(“Multiplication=”.$b.”<br>”);

Sb/-Sc;

echo(“Division=”.Sb.” <br>’);

Sb%-3;

echo(“Modulo=”.Sb.”<br>”);

5. Conditional or Ternary Operator –

Ternary operator starts with conditional expression followed by ? operator. Second part z(after ? and before : operator) will be executed if condition turns out to be true. If condition becomes false then third part (after 🙂 will be executed.

<?php

Sa = 10;Sb = 5;

Se = Sa> Sb? Sa: Sb;

echo(“Value of C=Se”);

?>

6. Increment or Decrement Operator:

These operators are also known as unary operator, means it has only single operand. This operator will increment or decrement the value of operand by 1, if you use ++ sign then it will add 1 in actual value, if you used — then it will decrease the value by 1 in actual
value. We can add this operator before or after the operand.

  • if you use the ++t operator as prefix like: ++ variable. The value of variable is incremented by 1 then, it returns the value
  • If you use the ++ operator as postfix like: variable ++, The original value of variable is returned first then, variable is incremented by 1.

<?php

Sa=5;

echo Sa-t;

Se=Sa;

echo Sc;

-Sc;

Sd=Sc;

echo(Sd);

?>

7. Concatenation operator:

In PHP for merge two string , to merge string with number or to merge string with variable we use ‘” operator. This operator always merge string data with other data types.

<?php

echo (“hello”. “Student”);

echo (Your Registration no=”.123);

Sa=10; Sb-“Diya Patel”;

echo(Sb.” your age-“.Sa);

?>

GIVE YOUR RATE

    YOU CAN WATCH VIDEO.

    Comments

    No comments yet. Why don’t you start the discussion?

    Leave a Reply

    Your email address will not be published. Required fields are marked *