Operators in Php


An operator is a symbol that represent a specific action.Operators are used to perform operations on variables and values.

Types of operators in php
1.) Arithmetic Operator
2.) Comparison Operator
3.) Logical Operator
4.) Assignment operator
5.)  Conditional Operator

1. Arithmetic Operator
In Arithmetic Operators we perform
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus  (to calculate reminder)
Example
<!DOCTYPE html>
<html>
<head><title>expertcodingmaster.com</title>
</head>
<body>
<?php
$a = 20;
$b = 10;
echo $c = $a + $b . "<br>";
echo $c = $a - $b . "<br>";
echo $c = $a * $b . "<br>";
echo $c = $a / $b . "<br>";
echo $c = $a % $b . "<br>";
?>
</body>
</html>

2. Comparison Operator
Comparison Operator is used to compare between two and more variables and values . In Php double equal to  (== ) sign is used used as compression operator.
Example
<!DOCTYPE html>
<html>
<head><title>expertcodingmaster.com</title>
</head>
<body>
<?php
$a = 20;
$b = 20;
if($a == $b)
{
echo "same";
}
else
{
echo "note same";
}
?>
</body>
</html>

3.) Logical Operators
In Logical Operator we perform
a) Logical AND operator
b) Logical OR operator
c) Logical NOT operator

a) Logical AND operator
In logical AND operator we check more than one conditional and all the condition must get true if we want to execute the statement. The best example of logical AND operator login system. In php double am-percent ( && ) sign is used as logical AND operator.
Example
<!DOCTYPE html>
<html>
<head><title>expertcodingmaster.com</title>
</head>
<body>
<?php
$username = 'expertcodingmaster';
$password = 123456;
if($username =='expertcodingmaster' && $password ==123456)
{
echo "welcome to expertcodingmaster.com";
}
else
{
echo "check your username and password";
}
?>
</body>
</html>

b) Logical OR operator
In logical OR operator if any condition get true then it will execute the statement. In Php double pipe ( || ) sign is used as logical OR operator.
Example
<!DOCTYPE html>
<html>
<head><title>expertcodingmaster.com</title>
</head>
<body>
<?php
$username = 'expertcodingmaster';
$password = 123456;
if($username =='expertcodingmaster' || $password ==1)
{
echo "welcome to expertcodingmaster.com";
}
else
{
echo "check your username and password";
}
?>
</body>
</html>
Unlike AND operator we don't need to get both the condition true. If any one of these get true it will execute the if block. Else block are only executed if both the condition are get false.

c) Logical NOT operator
In Php exclamation mark ( ) is used as logical OR operator.
Example
<!DOCTYPE html>
<html>
<head><title>expertcodingmaster.com</title>
</head>
<body>
<?php
$a = "Hello world";
$b = "";
if(!empty($a))
{
echo "The variable A has some value";
}
else
{
echo "The variable A no value";
}
if(!empty($b))
{
echo "The variable B has some value";
}
else
{
echo "The variable B has no value";
}
?>
</body>
</html>

4. Assignment operator
Assignment Operator is used to assign value to any variable. We can assign the value directly to the variable or after performing any operation we can assign value to variable. To assign value to variable '=' sign is used.
Example
<!DOCTYPE html>
<html>
<head><title>expertcodingmaster.com</title>
</head>
<body>
<?php
$a = 20;    // direct assignment
$b = $a + 10;  // assignment after operation
echo $b;
?>
</body>
</html>
Don't get confused while using single equal to ( = ) sign or double equal to ( == ) sign. Single equal to sign is used for assignment but double equal to sign is used for compression

5.)  Conditional Operator
Conditional operator contains
<   Less Than
>   Great Than
<=  Less Than Equal to
>=   Great Than Equal to
Example
<!DOCTYPE html>
<html>
<head><title>expertcodingmaster.com</title>
</head>
<body>
<?php
$Mukesh = 30;
$Ankita = 10;
$Prashant = 20;
if($Ankita < $Mukesh)
{
 echo "The marks of Mukesh is more than Ankita. <br>";
}
else
{
 echo "The marks of Ankita is more than Mukesh. <br>";
}
if($Ankita > $Prashant)
{
 echo "The marks of Ankita is more than Prashant. <br>";
}
else
{
 echo "The marks of Prashant is more than Ankita. <br>";
}
if($Mukesh <= ($Ankita + $Prashant))
 {
 echo "The marks of Mukesh is less than or equal to the sum of marks of Ankita and Prashant. <br>";
}
else
{
 echo "The marks of Mukesh is great than or equal to the sum of marks of Ankita and Prashant. <br>";
}
?>
</body>
</html>

No comments: