Nested if statements in PHP

Nested if statements means an if block inside another if block. Shortly a control structure inside another control structure.

It structure will look like

if (expression 1 )
{
if (expression 2 )
{
// statements 1
}
else
{
// Statements 2
}
}
else
{
if ( expression 2)
{
// Statements 3
}
else
{
// Statements 4
}
}

Here we can see another if .. else structure inside the if block and else block. Like this we can add any number of nested if else statements.

Nested if statements will make the PHP codes more comples and lengthy. To avoid Nested statements we can opt for elseif statements

elseif statement in PHP

“elseif” statement is used to execute several blocks of if statements. If we want to execute a different statement if the original if statement return FALSE we can use elseif control statement.

Its structure will look like
if (expression 1 )
{
// operation statements 1
}
elseif (expression 2 )
{
// operation statements 2
}
elseif (expression 3 )
{
// operation statements 3
}
else
{
// operation statements 4
}

First the main if statement will execute and if it return FALSE , then “operation statements 1” will be avoided and control will go to the next elseif statement with expression 2. If “expression 2” return FALSE then “operation statements 2” will be avoided and control will go to the next elseif statement with expression 3. If “expression 3” return TRUE then “operation statements 3” will be executed otherwise control will go to else statement and “operation statements 4” will be executed.

See the sample PHP program with elseif statement

<?php
$x = 10;
if ( $x < 5 )
{
  echo " value is less than 5";
}
elseif ( $x < 10 )
{
echo "value is less than 10";
}
elseif ( $x < 15 )
{
echo "value is less than 15";
}
else
{
echo " all the above checking failed and else statement executed";
}

?>

Here the ouput will be
value is less than 15

First if statement will execute ( 10 < 5 ) and surely it will return FALSE.
Then control will go to the next elseif statement ( 10 < 10 ) , and of course it also return FALSE.
Then control will go to the next elseif statement ( 10 < 15 ) and it will return TRUE and the block of code inside that elseif statements will execute and it will display the result “value is less than 15”

Note : Space between elseif statement ( like “else if” ) wont parse by PHP compiler and will return error. Make sure there is no space in elseif in your PHP programs.

else statement in PHP

“else” statement is an extension with “if” statement in PHP programming. If need to execute a block of statements when if statement return FALSE, we use else statement combine with if statement.

Here is the structure of a PHP program with else .
if (expression )
{
// execute statements if expression returns TRUE
}
else
{
// execute statements if expression returns FALSE
}

See the sample PHP program with else statement

<?php
$x = 10;
if ( $x < 5 )
{
 echo " value is less than 5";
}
else
{
 echo " value is greater than or equal to 5";
}
?>

Output of this program will be :
value is greater than or equal to 5

In this case if statement will return FALSE ( because 10 < 5 statement will return FALSE ) , then else statement will execute and code inside the else block will execute and will display the result “value is greater than or equal to 5”.

Note : else statement will come only with if statement

if statement in PHP

“if” statement is one of the most important and most used construct in PHP programming.

Its syntax will look like

if  (expression)
{
// Operation statements
}

if statement’s return result will be a Boolean value like TRUE or FALSE.
If the expression with “if statement” returns TRUE, the Operation Statements will execute , otherwise it will ignore the statements.

See the sample PHP program with if statement

<?php
$x = 10;
if ( $x < 15 )
{
 echo " First Statement Executed ";
}
if ( $x > 15 )
{
 echo " Second Statement Executed ";
}
?>

Output of this program will be :
First Statement Executed

In this case ( $x < 15 ) will return TRUE because 10 is less than 15, so the code inside the if block will execute. ( $x > 15 ) will return FALSE and the code inside that if loop wont execute and wont display “Second Statement Executed” in result.

We can use the if statements in a nested structure with else , elseif statements.
See the next posts for learn about those.

Bitwise Operators in PHP

And Bit Operator : &
usage ( $x & $y ), Bits that are set in both $x and $y are set.

Or bit operator ( inclusive or ) : |
usage ( $x | $y ), Bits that are set in either $x or $y are set.

Xor bit operator ( exclusive or ) : ^
usage ( $x ^ $y ), Bits that are set in $x or $y but not both are set.

Not bit operator : ~
usage ( ~$x ), Bits that are set in $x are not set, and viceversa.

Shift Left bit operator : <<
usage ( $x << $y ), Shift the bits of $x $y steps to the left (each step means “multiply by two”)

Shift right bit operator : >>
usage ( $x >> $y ), Shift the bits of $x $y steps to the right (each step means “divide by two”)

Logical Operators in PHP programming

Logical Operators are using for comparing between Boolean values and will return a Boolean result. Boolean values are TRUE and FALSE.

And Operator : and
Usage : ( $x and $y ), It will return TRUE if both $x and $y are TRUE, otherwise it will return FALSE.

Or Operator : or
Usage : ( $x or $y ). It will return TRUE if $x or $y is TRUE, otherwise it will return FALSE

Xor Operator : xor
Usage : ( $x xor $y ) , It will return TRUE if either $x or $y is TRUE, but both should not be TRUE. It will return FALSE if both are TRUE or FALSE.

Not Operator : !
usage ( !$x) , It will return TRUE if $x is not TRUE

And Operator : &&
Usage : ( $x && $y ), It will return TRUE if both $x and $y are TRUE, otherwise it will return FALSE.

Or Operator : ||
Usage : ( $x || $y ). It will return TRUE if $x or $y is TRUE, otherwise it will return FALSE

Here we can see that two operators are for And Operator ( and , && )
and
two operators are for Or Operator ( or, || ).
Both will do the same job.

Comparison Operators in PHP programming

Comparison Operators are used for compare two values.There are several types of comparison operators in PHP programming. See the details below.

Equal Operator : ==
Usage : ($x==$y) , It will return TRUE if value of $x is equal to $y, otherwise it will return FALSE.

Identical Operator : ===
It will check the value and type. Both should be same.
usage : ($x===$y), It will return TRUE if value and type of $x is same as value and type of $y, otherwise it will return FALSE.

Not Equal Operator : !=
Usage : ( $x != $y ), It will return TRUE if value of $x is not equal to value of $y, otherwise it will return FALSE.

Not Equal Operator : <>
Usage : ( $x <> $y ), It will return TRUE if value of $x is not equal to calue of $y, otherwise it will return FALSE.

Not Identical Operator : !==
Usage : ($x !== $y ), It will return TRUE if value of $x not equal to value of $y or type of $x is not same as type of $y.

Less than Operator : <
Usage ( $x < $y ), It will return TRUE if value of $x is less than the value of $y, Otherwise it will return FALSE.

Greater than Operator: >
Usage ( $x > $y ), It will return TRUE if value of $x is greater than the value of $y, Otherwise it will return FALSE.

Less than or Equal to Operator : <=
Usage ( $x <= $y ), It will return TRUE if value of $x is less than or Equal to value of $y, Otherwise it will return FALSE.

Greater than or Equal to Operator : >=
Usage ( $x >= $y ), It will return TRUE if value of $x is greater than or equal to value of $y, Otherwise it will return FALSE

These are the main comparison Operators in PHP. You should be very thorough about all these, because its usage is very much needed in our PHP script programming.

PHP Value Assignment Operators

Here we can learn about the operators used for assigning values in PHP.

Operator “=”
Usage Sample program

<?php
$x = 10;
$y = $x;
?>

Operator “+=”
x +=y is same as x = x + y
Usage Sample program

<?php
$x = 10;
$y = 20;
$x += $y;
echo $x ;
?>

Output of this PHP program will be :
30

Operator “-=”
x -=y is same as x = x – y
Usage Sample program

<?php
$x = 30;
$y = 20;
$x -= $y;
echo $x ;
?>

Output of this PHP program will be :
10

Operator “*=”
x *=y is same as x = x * y
Usage Sample program

<?php
$x = 5;
$y = 6;
$x *= $y;
echo $x ;
?>

Output of this PHP program will be :
30

Operator “/=”
x /=y is same as x = x / y
Usage Sample program

<?php
$x = 10;
$y = 5;
$x /= $y;
echo $x ;
?>

Output of this PHP program will be :
2

Operator “.=”
x .=y is same as x = x . y ( Concatenation operation )
Usage Sample program

<?php
$x = "Syam";
$y = "Here";
$x .= $y;
echo $x ;
?>

Output of this PHP program will be :
Syam Here

Operator “%=”
x %=y is same as x = x % y ( Division Remainder )
Usage Sample program

<?php
$x = 10;
$y = 3;
$x %= $y;
echo $x ;
?>

Output of this PHP program will be :
1

Arithmetic calculation operators in php

There are several operators in PHP available for arithmetic calculations and all.

Addition Operator : +
Use : For the addition of two numbers.
Example

<?php
$a = 30;
$b = 10;
$c = $a + $b;
echo $c;
?>

Output will be ( 30 + 10 ):
40

Subtraction Operator : –
Use : Subtract one value from another value
Example

<?php
$a = 30;
$b = 10;
$c = $a - $b;
echo $c;
?>

Output will be ( 30 – 10) :
20

Multiplication Operator : *
Use: Mutiply one value with another
Example

<?php
$a = 30;
$b = 10;
$c = $a * $b;
echo $c;
?>

Output will be ( 30 * 10) :
300

Division Operator : /
Use : for the division of one value by another
Example

<?php
$a = 30;
$b = 10;
$c = $a / $b;
echo $c;
?>

Output will be ( 30 / 10) :
3

Modulus Operator : %
It will return the division remainder
Example 1:

<?php
$a = 30;
$b = 10;
$c = $a % $b;
echo $c;
?>

Output will be remainder of 30 / 10 :
0

Example 2:

<?php
$a = 5;
$b = 2;
$c = $a % $b;
echo $c;
?>

Output will be remainder of 5 / 2) :
1

Example 3:

<?php
$a = 10;
$b = 4;
$c = $a % $b;
echo $c;
?>

Output will be the remainder of 10 / 4 :
2

Increment Operator : ++
Use: Its a short code for using against + 1
Example

<?php
$a = 30;
$a++;
echo $a;
?>

Output will be ( 30 + 1 ):
31

Decrement Operator : —
Use: Its a short code for using against – 1
Example

<?php
$a = 30;
$a--;
echo $a;
?>

Output will be ( 30 – 1 ):
29

Negation Operator : will return the opposite of number
Example

<?php
$a = 30;
$b = -$a;
echo $b;
?>

Output will be :
-30

strpos() – PHP string first occurance position

PHP strpos function is using for checking the occurrence position of a character or string within a string.It will return the numeric position number of the first occurrence of the searched string.
If strpos function failed to find the searched string , it will return “FLASE”.

For example if we want to know the position of letter “M” in word “MALAYALAM” , strpos function will return the value 0 ( zero ) . It means the searched letter’s position is first on that string.
Please note : Note count is starting from 0 (zero) not from 1 (one )

See the following sample PHP program with strpos function

<?php
$main_string="MALAYALAM";
$search_string="M";
$position  = strpos($main_string, $search_string);
if ($pos==false)
 {
  echo "Searched string was not found in the main string";
 }
else
 {
 echo "found at position ".$position ;
 }
?>

The output of this PHP script will be :
found at position 0

See another example

<?php
$main_string="Hello world";
$search_string="world";
$position  = strpos($main_string, $search_string);
if ($pos==false)
 {
  echo "Searched string was not found in the main string";
 }
else
 {
 echo "found at position ".$position ;
 }
?>

The output of this PHP script will be :
found at position 6

Ok. Now we got the idea about how to get a string’s first occurrence position in another string. But in some situations we need to get the first occurrence position with a offset. It means if need to search the occurrence of a string from a fixed position of the main string than from its starting. ( like we need to start the search from position 3 and avoid first 4 position of the main string ).
You will get a clear picture after seeing the following sample program

<?php
$main_string="MALAYALAM";
$search_string="M";
$position  = strpos($main_string, $search_string, 1);
if ($pos==false)
 {
  echo "Searched string was not found in the main string";
 }
else
 {
 echo "found at position ".$position ;
 }
?>

Output of this program will be
found at position 8

By using the optional offset parameter 1 with strpos function , search will ignore anything before the specified offset value 1 in the main string. So in this example the first M in string MALAYALAM wont consider because it is before the specified offset value 1.

Share
Share