Conditional Statement

Following are the  conditional statement in PHP.
1 if
2 if else
3 nested if else

Syntax  1:

if(condition){
statement;
}

Note : In the above case if condition true then statement will execute otherwise execution will skip this part.

Example :
<?php
$a =100;
if($a>=100){ // If condition true
echo "Value of a=$a";
}
?>
Output : Value of a=100;

<?php
$a =100;
if($a<100){ // In this case condition fail thats why execution skip this block
echo "Value of a=$a";
}
?>
Output : Nothing

Syntax 2:

if(condition){
statement1;
}else{
statement2;
}
Note: In the above case if condition is true then if statement will execute otherwise else statement will execute.

Example 1 :
<?php

$a=100;
if($a!=100){
echo "Satatement1 : value of a=$a";
}else{
echo "Satatement2 : value of a=$a";
}
?>

Output : Statement2 : value of a = 100

Example 2 :
<?php

$a=99;
if($a!=100){
echo "Satatement1 : value of a=$a";
}else{
echo "Satatement2 : value of a=$a";
}
?>

Output : Statement1 : value of a = 99

Syntax 3 :

if(condition){

Statement1;

}elseif(condition){

Statement2;

}elseif(condition){

statement3;

}else{

statement4

}

Note : The above statement is nested statement so there is no limit of hierarchy so according to requirement we can either increase or decrease it .In this hierarchy if you have only conditional statement then there is no need to use default statement 'else'. In this case "else" statement is optional.

Example :

<?php

$marks = 40;
if($marks>=60){

echo "First division";

}elseif($marks<60 && $marks>=40){

echo "Second division";

}elseif($marks<40 &&  $marks>=30){

echo "Third division";

}else{

echo "Fail";

}

?>

Output : Second division

Note : So finally if you have a multiple conditions then you can manage your requirement using nested 'if else statement'.

Concept of variables in php

- First character must be start with $

- Second character should be start with either alphabet or underscore like ($_ , $a etc)

- Space are not allowed.If you try to use space in variables name then PHP will throw parse error like below:

Parse error: syntax error, unexpected 'b' (T_STRING)

- Special characters are not allowed except _ .So if you try to put special characters in php variables name then it always throw  Parse error.

- You can use numbers after second character.Example ( $_123,$a123,@a_b123) these are the correct variable name.

Examples :

Correct : $_a, $a_fsdf , $_123, $a1nb etc

Wrongaccount, $1asd ,$aaa$df, $test abc etc