PHP Operators, Types of Operators, Operators with examples in PHP

Hello Friend how are you i hope you all are doing well. Today in this post we will learn about operators like what is operator , what is operand , Types of operators and each types with examples. If you want to learn PHP in very simple easy way then this post will help you definitely.


👉Operator

It is a special symbol which is used to perform logical or mathematical operation on data or variable.

👉Operand
It is a data or variable on which the operation is to be performed.

👉Types of Operator
⇒Arithmetic Operators
⇒Relational Operators
⇒Logical Operators
⇒Assignment Operators
⇒Bitwise Operators
⇒Increment/Decrement Operators
⇒Conditional Operators

👉Arithmetic Operators

SymbolOperationExample
+Additionx+y
-Subtractionx-y
*Multiplicationx*y
/Divisionx/y
%Modulusx%y
/*Example*/
<?php
$x=13;
$y=5;

echo "Add=".($x+$y)."<br>";
echo "Sub=".($x-$y)."<br>";
echo "Multi=".($x*$y)."<br>";
echo "Div=".($x/$y)."<br>";
/*modulus(%) always holds remainder*/
echo "Mod=".($x%$y)."<br>";

/*
Output
Add=18
Sub=8
Multi=65
Div=2.6
Mod=3
*/
?>

👉Relational Operators

SymbolOperationExample
==Equal to2==3 returns 0
!=Not equal to2!=3 returns 1
>Greater than2>3 returns 0
<Less than2<3 returns 1
>=Greater than or equal to2>=3 returns 0
<=Less than or equal to2<=3 returns 1

👉Logical Operators



andIt is called Logical AND operator. If both the operands are true then condition becomes true.
orIt is called Logical OR Operator. If any of the two operands are non zero then condition becomes true.
(x>y)&&(x>z)Here this expression returns true if both conditions are true.
(x>y)||(x>z)Here this expression returns true if any one or both conditions are true.
!(x>y)Not operator reverses the state means if the condition is true it returns false and if the condition is false it returns true.
/*Example*/
<?php
/*Show result according to percent*/
$marks=55;
if($marks>=60)
{
//this part will execute if percent is greater than  or equal to 60
echo "First division<br>";
}
if($marks<60 && $marks>=45)
{
//this part will execute if percent is between 59 and 45
echo "Second division<br>";
}
if($marks<45 && $marks>=33)
{
//this part will execute if percent is between 44 and
echo "Third division<br>";
}
if($marks<33)
{
//this part will execute if percent is less than  33
echo "Sorry!! You are Fail!!<br>";
}
?>
//output
Second division

👉Assignment Operators

SymbolExampleSame as
=x=yx=y
+=x+=yx=x+y
-=x-=yx=x-y
*=x*=yx=x*y
/=x/=yx=x/y
%=x%=yx=x%y
<?php
$x1=13;$y1=5;
$x1+=$y1;/*x1=x1+y1*/
echo "Add=".$x1."<br>";

$x2=13;$y2=5;
$x2-=$y2;/*x2=x2-y2*/
echo "Sub=".$x2."<br>";
/*
Output
Add=18
Sub=8
*/
?>

👉Bitwise Operators

SymbolOperationExample
& Bitwise ANDx&y
|Bitwise ORx|y
<<Shift Leftx<<2
>>Shift Rightx>>2
^X-OR x^y
<?php
$x=3;
$y=5;
echo "x&y=".($x&$y)."<br>";
echo "x|y=".($x|$y)."<br>";
echo "x>>1=".($x>>1)."<br>";
echo "x<<2=".($x<<2)."<br>";
echo "x^y=".($x^$y)."<br>";
/*
Output
x&y=1
x|y=7
x>>1=1
x<<2=12
x^y=6
*/
?>

👉Increment/Decrement Operators

SymbolNameFunctionExample
++IncrementIt increments the value by 1++x
--DecrementIt decrements the value by 1--x
<?php
$x=3;
$y=5;
echo "++x=".++$x."<br>";
echo "--y=".--$y."<br>";
/*
Output
4
4
*/
?>

👉Conditional Operators

  • If the condition is true second part will execute otherwise third part will execute.
<?php
$x=3;
$y=5;
$str=$x>$y?"x is greater":"y is greater";
echo $str;
/*
Output
y is greater
*/
?>

Request:-If you found this post helpful then let me know by your comment and share it with your friend. 

If you want to ask a question or want to suggest then type your question or suggestion in comment box so that we could do something new for you all. 

If you have not subscribed my website then please subscribe my website. Try to learn something new and teach something new to other. 

Thanks

Post a Comment

1 Comments

  1. VEGAS (REALTOR SHIPSTER) - YouTube
    VEGAS (REALTOR SHIPSTER) · Song · youtube to mp3 download Videos · Videos · Translate this page YouTube · Facebook · Twitter · Tumblr · Pinterest.

    ReplyDelete