

Operator’s screenshots source: https://www.php.net/manual/en/language.operators.php


Let’s see operator operations…
- Unary Operator
We found increment and decrement cases.
Increment case: a++ or ++a // a++ means a=a+1
Decrement case: a– or –a// a– means a=a-1
File – incr.php
<?php $a=10; echo “<br> a is $a”; // a++ means a=a+1 $a++; echo “<br> a is $a”; ++$a; echo “<br> a is $a”; ?> |

File – decr.php
<?php $a=10; echo “<br> a is $a”; // a– means a=a-1 $a–; echo “<br> a is $a”; –$a; echo “<br> a is $a”; ?> |

- Arithmetic Operators: operators including (+ – * / % **) have several roles in PHP for doing arithmetic operations.
Let’s see some, Arithmetic operations in PHP
let’s perform addition operation in PHP.
File – code1.php
<?php // addition operator ‘+’ $b=10; $c=20; $add=$b+$c; echo ‘addition is: ‘.$add; ?> <?php $k=20; $k++; echo ‘<br>’; echo ‘k is: ‘.$k; ?> |

let’s perform subtraction operation in PHP.
File – code2.php
<?php // subtraction operator ‘-‘ $b=10; $c=20; $sub=$c-$b; echo ‘subtraction is: ‘.$sub; ?> <?php $k=20; $k–; echo ‘<br>’; echo ‘k is: ‘.$k; ?> |

let’s perform multiplication operation in PHP.
File – code3.php
<?php // multiplication operator ‘*’ $b=10; $c=20; $mul=$c*$b; echo ‘multiply is: ‘.$mul; ?> |

file – code4.php
<?php // division operator ‘/’ $b=10; $c=20; $div=$c/$b; echo ‘division is: ‘.$div; ?> <?php echo ‘<br> 200/10 is:’; echo 200/10; ?> |

let’s perform modulus operation in PHP.
File – code5.php
<?php // modulus operator ‘%’ $b=10; $c=20; $mod=$c%$b; echo ‘modulus is: ‘.$mod; ?> <?php echo ‘<br> 10%7 is: ‘; echo 10%7; ?> |

file – code6.php
<?php /* write a code, to find the quotient and remainder of two values */ $a=10; $b=7; $quo=$a/$b; $rem=$a%$b; echo ‘quotient of 10 & 7 is: ‘.$quo; echo ‘<br> remainder of 10 & 7 is: ‘.$rem; $a=20; $b=2; $quo=$a/$b; $rem=$a%$b; echo ‘<br><br> quotient of 10 & 7 is: ‘.$quo; echo ‘<br>remainder of 10 & 7 is: ‘.$rem; ?> |

File – code7.php
<?php /* Exponentiation operator ‘**’ 3 ** 2 = 3^2 = 3×3 = 9 4 ** 2 = 4^2 = 4×4 =16 */ echo ‘3**2 is: ‘; echo 3**2; echo “<br>”; echo ‘4**2 is: ‘; echo 4**2; ?> |

- Logical operators: logical operators including [AND (&& and) OR (|| or) NOT (!)]
It checks both condition is true or not… if true then it returns ‘1’, if false then return ‘0’
Let’s see some coding examples using logical operators …
let’s perform Logical AND operation in PHP.
File – code8.php
<?php /* logical AND true && true true and true */ $a=10; $b=20; if ($a==10 && $b==20) { echo ‘variable values $a,$b matched!’; } $str1=’microcodes’; $str2=’microcodes.in’; if ($str1==’microcodes’ and $str2==’microcodes’) { echo ‘<br> both strings matched!’; } else{ echo ‘<br> strings not matched!’; } ?> |

let’s perform Logical OR operation in PHP.
file – code9.php
<?php /* logical OR true || or_true true or or_true */ $a=10; $b=20; if ($a==30 || $b==20) { echo ‘one variable matched!’; } $str1=’microcodes’; $str2=’microcodes.in’; if ($str1==’shimanta’ or $str2==’microcodes.in’) { echo ‘<br> one string matched!’; } else{ echo ‘<br>both strings not matched!’; } ?> |

let’s perform Logical NOT operation in PHP.
file – code10.php
<?php // NOT operator ‘!’ $a=10; echo $a!=9; $str=’krishna’; if($str!=’krish’){ echo ‘<br> str not krishna ‘; } ?> |

file – code11.php
<?php /* xor gate means xor means one either true but both not */ $a=2; $b=3; if($a==3 xor $b==3){ echo ‘variable a & b may matched! ‘; } $str1=’TATA’; $str2=’TATA’; if($str1==’TATA’ xor $str2==’TATA’){ echo ‘<br>TATA IS TATA’; } else{ echo ‘<br> else part runs!!’; } ?> |

- Relational operators: including operators [>= <= != ><]
Let’s see some coding examples using relational operators…
let’s perform relational(less than < ) operation in PHP.
File – code12.php
<?php // less than operand_1 < operand_2 $a=10; $b=11; if($a<$b){ echo ‘a is smaller than b’; } ?> |

let’s perform relational(greater than > ) operation in PHP.
file – code13.php
<?php // greater than operand_1 > operand_2 $a=20; $b=11; if($a>$b){ echo ‘a is greater than b’; } ?> |

let’s perform relational(less than equal <= ) operation in PHP.
file – code14.php
<?php // less than or equal , operand_1 <= operand_2 $a=20; if($a<=21){ echo ‘a is <= 20’; } $b=$a; if($b<=20){ echo ‘<br> b is <= 20’; } ?> |

let’s perform relational(greater than > ) operation in PHP.
file – code15.php
<?php // greater than or equal , operand_1 >= operand_2 $a=20; if($a>=19){ echo ‘a is >= 19’; } $b=$a; if($b>=20){ echo ‘<br> b is >= 20’; } ?> |

- Conditional (ternary) Operator:
Syntax – expression_1? expression_2:expression_3
Here if ‘expression_1’ matches then ‘expression_2’ will executes, otherwise ‘expression_3’ executes.
File – code16.php
<?php $v1=20; echo ‘variable v1 is: ‘.$v1; $v1 = ($v1==20)?300:200; echo ‘<br> variable v1 now: ‘.$v1; ?> |

file – code17.php
<?php $str=’Jamshed Ji TATA’; echo ‘str is: ‘.$str; // see variable $str can’t matched $str = ($str==’Jamshed TATA’)?’JRD TATA’:’Ratan TATA’; echo ‘<br> str now: ‘.$str; ?> |
