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
Symbol | Operation | Example |
+ | Addition | x+y |
- | Subtraction | x-y |
* | Multiplication | x*y |
/ | Division | x/y |
% | Modulus | x%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
Symbol | Operation | Example |
== | Equal to | 2==3 returns 0 |
!= | Not equal to | 2!=3 returns 1 |
> | Greater than | 2>3 returns 0 |
< | Less than | 2<3 returns 1 |
>= | Greater than or equal to | 2>=3 returns 0 |
<= | Less than or equal to | 2<=3 returns 1 |
👉Logical Operators
and | It is called Logical AND operator. If both the operands are true then condition becomes true. |
or | It 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
Symbol | Example | Same as |
= | x=y | x=y |
+= | x+=y | x=x+y |
-= | x-=y | x=x-y |
*= | x*=y | x=x*y |
/= | x/=y | x=x/y |
%= | x%=y | x=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
Symbol | Operation | Example |
& | Bitwise AND | x&y |
| | Bitwise OR | x|y |
<< | Shift Left | x<<2 |
>> | Shift Right | x>>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
Symbol | Name | Function | Example |
++ | Increment | It increments the value by 1 | ++x |
-- | Decrement | It 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.
1 Comments
VEGAS (REALTOR SHIPSTER) - YouTube
ReplyDeleteVEGAS (REALTOR SHIPSTER) · Song · youtube to mp3 download Videos · Videos · Translate this page YouTube · Facebook · Twitter · Tumblr · Pinterest.