Complete Guide to PHP Operators with Examples for Beginners
Operator in php:
Complete Guide to PHP Operators are special symbol for the purpose to make actions performed by the user .
There are seven operators in php.those are
1.Arthematic operator
2.Assignment operator
3.logical operators
4.comparision operators
5.string operators
6.increment/decrement operators
Arthematic operators:
arthematic operators used for mathematical calculation using operands.
+ Addition
– Subtraction
* Multiplication
/ Division
% Modulus
Program using arthematic operators in php:
<?php
$a=60;
$b=30;
echo “addition : “.($a+$b).”\n”; //addition
echo “sub : ” . ($a-$b).”\n”; //Subtraction
echo “mul : “. ($a*$b) .”\n”; //multiplication
echo “div : ” . ($a/$b). “\n”; //division
echo “mod : “. ($a%$b).”\n”; //modulus
?>
Output1:
addition: 90
sub : 30
mul :1800
div : 2
mod : 0
Program 2 with different inputs:
<?php
$a=80;
$b=20;
echo “addition : “.($a+$b).”\n”; //addition
echo “sub : ” . ($a-$b).”\n”; //Subtraction
echo “mul : “. ($a*$b) .”\n”; //multiplication
echo “div : ” . ($a/$b). “\n”; //division
echo “mod : “. ($a%$b).”\n”; //modulus
?>
Output 2:
addition : 100
sub : 60
mul :1600
div : 4
mod :0
Assignment operators in php:
These assignment operators are used to perform some operations and that value is stored in that assignment variable.this is similar to arthematic operators but the difference is we can the value in specific assigned variable.
Symbols Explanation
= Assignment
+= Addition and assignment
-= Subtraction and assignment
*= Multiplication andassignment
/= Division and assignment
%= Modulus and assignment
program for assignment operators using php:
<?php
$a=50;
echo $a.”\n”;
//Addition and assignment
$b=40;
$b +=20;
echo $b;
//Subtraction and assignment
$b=40;
$b -=20;
echo $b.”\n”;
//Multiplication and assignment
$b=40;
$b *=20;
echo $b.”\n”;
//Division and assignment
$b=40;
$b /=20;
echo $b.”\n”;
//Modulus and assignment
$b=40;
$b %=20;
echo $b.”\n”;
?>
Output:
50
60
20
800
2
0
Program 2 with different outputs:
?php
$a=100;
echo $a.”\n”;
//Addition and assignment
$b=92;
$b +=20;
echo $b;
//Subtraction and assignment
$b=60;
$b -=12;
echo $b.”\n”;
//Multiplication and assignment
$b=13;
$b *=13;
echo $b.”\n”;
//Division and assignment
$b=60;
$b /=30;
echo $b.”\n”;
//Modulus and assignment
$b=20;
$b %=10;
echo $b.”\n”;
?>
Output 2:
I00
112
48
169
2
0
Logical operators in php:
Logical operators are used to check the condition and return the value.logical operators are used major programs.it is very important concepts in operators without these logical operators we cannot find the way to check both conditions at the time.
Symbols. Explanation
&& Logical AND // if both conditions are true that the answer is true
|| Logical OR// if one condition is true then the answer is true
! Logical not // if condition is true the answer is false.
Program for logical operators in php:
<?php
$a=40;
$b=50;
//Logical and
if(($a>20) && ($b>40))
echo “statements are true”.”\n”;
//Logical OR
if(($a>20)||($b>40))
echo “statements are true”.”\n”;
//Logical not
if (!$a=30)
echo “false”;
?>
output:
statements are true
Statement are true
false
Comparison operators in php:
Comparison operators are used to check the condition wheather it is satisfy or not.
There many operators.in comparison.those are greater than,less than,equal too,greater than are equal,less than or equal,not equal.
Symbols. Explanation
Equal to//$a==10
< Less than //$a<10
> Greater than // $a>20
<= Less than are equal to //$a<=20
>= Greater than are equal to
//$a>=40
Increment/decrement operators in php:
Increment/decreament operators are used for increment value by 1 or decreament value by 1.
Once the value is assigned after some time we need to increase the variable value.then we are using this increment and decrement operators.
Symbol. Explanation
++ Increment
— decrement
Program for increment/decrement operators in php:
<? php
$a=2;
echo $a++;
echo $a–;
echo ++$a;
echo –$a;
?>
String operators in php
String are are used in php for concatenation.
. Symbol Indicare for concatenation.
2 thoughts on “Complete Guide to PHP Operators with Examples for Beginners”