If/Else Statement in Php


An If statement can be followed by an optional else statement, which executes when the Boolean expression is false.

The syntax of if....else statement in Php is

if(condition)
{
    /* statements will execute if the condition is true */
}
else
{
    /* statements will execute if condition will false */
}

If all the conditions get true then 'if' block will be executed, otherwise 'else' block will be executed.

Example :-
<!DOCTYPE html>
<html>
<head><title>expertcodingmaster.com</title>
</head>
<body>
<?php
$a = 20;   
$b = 10;   
if($b > $a)
{
echo "B is greater";
}
else
{
echo "A is greater";
}

?>
</body>
</html>

Output of the above code
A is greater



Ternary Operator in Php


Ternary operator is simply short form of if/else statement. Ternary operator is also known as SHORTHAND operator.
The logic of using the Ternary operator is  " (condition) ? true statement : false statement "
Example :-
<!DOCTYPE html>
<html>
<head><title>expertcodingmaster.com</title>
</head>
<body>
<?php
$a = 20;   
$b = 10;   
$example = ($b > $a) ? "B is greater" : "A is greater";
echo $example;
?>
</body>
</html>

Output of the above code
A is greater

Difference between double equal to (==) and triple equal to (===) compression operator in Php


Double equal to (==) and triple equal to (===) both are use as compression operator in Php. But there is big difference between double equal to (==) compression operator and triple equal to (===) compression operator.
Double equal to (==) compression operator can only compare the value but triple equal to (===) compression operator compare the value as well as data type of the value. This will be completely clear while seeing the below example.

Example :-
Comparing two value using double equal to compression operator
<!DOCTYPE html>
<html>
<head><title>expertcodingmaster.com</title>
</head>
<body>
<?php
$a = 10;      // integer value
$b = "10";     // string value
if($a == $b)
{
echo "both values are same";
}
else
{
echo "both values are not same";
}
?>
</body>
</html>

Output of the above code
both values are same
The above example clearly shows that the variable  'b' contain string value but the output shows both the values are same.

Now the same example is performed using the triple equal to compression operator
<!DOCTYPE html>
<html>
<head><title>expertcodingmaster.com</title>
</head>
<body>
<?php
$a = 10;      // integer value
$b = "10";     // string value
if($a === $b)
{
echo "both values are same";
}
else
{
echo "both values are not same";
}
?>
</body>
</html>

Output of the above code
both values are not same
While using the triple equal to compression operator the output shows both the values are not same as it compare the value as well as data type of the value.



Conditional Operator in Php


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

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>

Output of the above code
The marks of Mukesh is more than Ankita.

The marks of Prashant is more than Ankita.

The marks of Mukesh is less than or equal to the sum of marks of Ankita and Prashant. 

Assignment operator in Php


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

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

Output of the above code
30



Logical Operator in Php


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

Logical Operators
There are three types of logical operators
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>

Output of the above code
welcome to expertcodingmaster.com


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.


The output of the above code
welcome to expertcodingmaster.com

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>

The output of the above code
The variable A has some value
The variable B has no value 

Comparison Operator in Php


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

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>

The output of the above code
same

Arithmetic Operator in Php


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

 Arithmetic Operators
The Arithmetic Operators contain the basic operation on
+      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>

The output of the above code
30
10
200
2
0

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>