Loops In PHP

Four types of loop in php:

  1. for
  2. foreach
  3. while
  4. do while

For Loop

Syntax 1:
for( Initialization ; Condition ; Increment/Decrement ){

Statement;

}
Example 1: Write a program to print value from 1 to 5.
<?php
for($initialization=1 ; $initialization<=5 ; $initialization++){
    echo $initialization."<br>";
}
?>
Output:
1
2
3
4
5

Syntax 2:
for( Initialization1 , Initalization2 ; Condition ;  Increment/Decrement ,  Increment/Decrement ){

Statement;

}
Example 2 : Write a program to print 1 to 5 as well as 5 to 1.
<?php
for($initialization1=1 , $initialization2 = 5; $initialization1<=5 ; $initialization1++ ,$initialization2-- ){
    echo $initialization1."&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$initialization2<br>";
}
?>
Output:
1     5
2     4
3     3
4     2
5     1

Syntax 3 :
Initialization;
for ( ; Condition ; Increment/Decrement ){

Statement;

}

Syntax 4 :
Initialization;
for( ; Condition ; ){

Statement;
Increment/Decrement;

}

While Loop:

Syntax 1:

 while(condition true){

statement1;

}

Example 1 :

<?php
$var = 5;
while($var!=0){
echo $var;
$var--;
}
?>

Output : 54321
Note : While loop work if and only if while condition is true.

Do While Loop:

Syntax 1:

do{

statement1;

}while(condition true)

Note : Suppose you have a situation in which you would like to execute one statement without condition and the same statement with condition then in this type of situation "do while" conditional statement is useful.

Example 1:

<?php
$var = 5;
do{
/**
* This statement will execute first time without any condition
* but next time execute with while condition.
*/
echo "value = $var<br>";
$var--;
}while($var!=0);
?>

Output :
value = 5
value = 4
value = 3
value = 2
value = 1

Operators in php

Php Operators :

There are many types of operators in php.

  1. Arithmetic Operators
  2. Comparison Operators
  3. Assignment Operators
  4. Increment/Decrement Operators
  5. Logical Operators
  6. String operators
  7. Error Control Operators
  8. Array Operators
  9. Bitwise Operators
  10. Type Operators
  11. Execution Operators
  12. Error Control Operators

1 Arithmetic Operators

+ (addition)
- ( subtraction) This operation work as a subtraction as well as negation
$a - $b ( this is the example of subtraction)
-$a ( this is the example of negation that means opposite of  $a )
* (multiplication)
% ( modulus
/ ( division)
** ( exponentiation )  ( This will work in PHP 5.6 version or later)

 Example of arithmetic operators :

<?php
$var1 = 100;
$var2 = 200;
$sum = $var1 + $var2;
$subtraction = $var2 - $var1;
$multiplication = $var1 * $var2;
$division = $var2/$var1;
$modulus = $var2%$var1;

echo "Sum  = $sum<br>";
echo "Subtraction  = $subtraction<br>";
echo "Multiplication  = $multiplication<br>";
echo "Division  = $division<br>";
echo "modulus = $modulus<br>";

?>
Output :
Sum = 300
Subtraction = 100
Multiplication = 20000
Division = 2
modulus = 0


2 Comparison Operators

==  ( Equal )
=== ( Identical )
!= (Not Equal )
!== ( Not Identical )
<> ( Not Equal )
>= ( Greater than or equal to )
<= ( Less than or equal to )
>  ( Greater than )
<  ( Less than )

Simple example of the above mentioned operators.

<?php
$var1 = 100;
$var2 = 100.00;
$var3 = 100.00;
$var4 = 50;
if($var1==$var2){
echo "Equal comparison operator<br>";
}
/**
* This condition work if and only if
*  both operands are same in value as well as data type
*  In this case $var1 and $var2 are same in value but not in data type.
*  So this condition will not work.
*/
if($var1===$var2){
echo "Identical comparison operator<br>";
}
/**
* This condition will work because $var2 and $var3 are same in value as well as data type
*/
if($var2===$var3){
echo "Identical comparison operator<br>";
}
if($var1 > 50){
echo "Greater than operator<br>";
}
if($var1 < 50){
echo "Less than operator<br>";
}
if($var1 >= 100){
echo "Greater than or equal operator<br>";
}
if($var1 <= 100){
echo "Less than or equal operator<br>";
}
if($var1 != $var4){
echo "Not equal operator<br>";
}
if($var1 <> $var4){
echo "Not equal operator<br>";
}
if($var1 !== $var2){
echo "Not identical operator<br>";
}
?>

Output :
Equal comparison operator
Identical comparison operator
Greater than operator
Greater than or equal operator
Less than or equal operator
Not equal operator
Not equal operator
Not identical operator


3 Assignment Operators

= ( This is assignment operator )

<?php
$a =100;
echo "Value of a = $a<br>";
$a+=100;    // $a = $a + 100;
echo "Value of a = $a<br>";
$a-=100;    //$a = $a-100
echo "Value of a = $a<br>";
$a*=100;     // $a = $a*100
echo "Value of a = $a<br>";
?>
Output :
Value of a = 100
Value of a = 200
Value of a = 100
Value of a = 10000


4 Increment/Decrement Operators

++ ( Increment Operator )
- -  ( Decrement Operator )

$a++   // $a++  and $a=$a+1 are same
$a--     // $a-- and $a=$a-1 are same

Understand with simple example.

1 )
<?php
$a = 100;
echo "Value of a = $a<br>";
/**
* Now you can increase the value of $a by 1
* using $a++( $a++ means $a=$a+1)
*/
$a++;
echo "After increment the value of a = $a<br>";
/**
* Now you can decrease the value of $a by 1
* using $a-- ( $a-- means $a=$a-1
*/
$a--;
echo "After decrement the value of a = $a<br>";
?>

Output :
Value of a = 100
After increment the value of a = 101
After decrement the value of a = 100


5 Logical Operators

There are three logical operators:
1) &&  ( And operator works on two operands  like $operand1 && $operand2)
2) ||  ( Or operator works on two operands like  $operand1 || $operand2)
3) !  ( Not operator  works on one operands like !$operand1)

Lets take a simple example to understand these all three operators.
You can copy past to check the blow code as well.

<?php

/**
 * 
 * In this block we will try to understand && logical operator
 */
$operand1=true;
$operand2=true;
if($operand1 && $operand2){  // If both are true then its work
    echo "Condition is true<br>";
}

$operand1=false;
$operand2=true;

if($operand1 && $operand2){  // If both are true then its work
    echo "If condition is true because of  both operands are true<br>";
}else{ //  If any of the operands or both operands are false then this block will work
    echo "Else block will execute because of any of the operands or both operands are false<br>";
}

/**
 * Output of the above block is below:
 * Condition is true
 * Else block will execute because of any of the operands or both operands are false
 */

/**
 * In this block we will try to understand ||  logical operator
 */

$operand1=false;
$operand2=true;

if($operand1 ||  $operand2){  // If any one operand is true then this block will work
    echo "Condition is true<br>";
}

$operand1=false;
$operand2=false;

if($operand1 || $operand2){  // If any one operand is true then this block will work
    echo 'If any one operand is true then this block will work<br>';
}else{ //  If  both operands are false then this block will work
    echo "Else block will execute because of both operands are false<br>";
}

/**
 * Output of the above block is below:
 * Condition is true
 * Else block will execute because of both operands are false
 */


/**
 * Lets take a example to understand ! (not) operator 
 */

$operand1=false;
if(!$operand1){   //  This means if not true then this block will work
    echo " This means if not true then this block will work<br>";
}

$operand1 =10;
if($operand1==10){
    echo "Operand1 value is 10<br>";
}
$operand1 =11;
if($operand1!=10){
    echo "Operand1 value is not 10<br>";
}

/**
 * Output of the above block is below:
 * This means if not true then this block will work
 * Operand1 value is 10Operand1 value is not 10
 */

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