Saturday 11 January 2014

PHP crash course -2

In this section we are going to discuss logic & loping in PHP . 



The if....Else statements :

Similar  to C program if ... else statements 
condition statements are used to perform different actions based on different condition.

 If statement :Use this to execute some code only if specify condition is true.
 If...Else statement : Use this condition to execute some code when condition is true & another code when condition is false .
Nested if..else : used in nested case .

this is a very basic Example of  If.. else statement . 
 <?php  
 //if .. else statement  
 $d=date("D"); // syntax to find day ;  
 if($d=="Sat")  
 echo " HAVE A NICE Weekend !";  
 else  
 echo " TODAY is : $d";  
 ?>  

Run This Program  to see Output .
Related Post : How to host your website for FREE (google drive )
Example for Nested If.. Else .
 <html>  
 <body>  
 <?php  
 // Hierarchy of if ... else statement  
 $d=date("D");  
 if($d=="Fri")  
   echo " Have a nice Weekend !";  
   elseif($d=="Sun")  
     echo "Have a Nice Sunday!";  
           else  
           echo "Have a Nice day ";  
 ?>  
 </body>  

Output of above to program is dynamic i.e changes day to day .. so run It & check this out.

Switch Statement : Use this statement to select one of many block of code to execute. An example are given there.


 <?php  
 // switch statement same as switch in to c language  
 $x=3;  
 switch($x)  
 {  
  case 1:  
    echo "Number 1";  
       break;  
  case 2:  
    echo "Number 2";  
       break;  
  case 3:  
     echo " NUmber 3";  
        break;  
  default :  
     echo " Please enter no. b/w 1 to 3";   
 }   
 ?>  

Now Its time For Looping .. loops in Php is same as that of C langauge .

While LOOPS : Loop execute a block of code a specified No of times, or while a specified condition is TRUE.

while(condition)
        {
           code to be execute ;
        }

Ex.


 <?php  
 //Looping While loop :  
 $i=0;  
 while($i<10)  
 {  
  echo "welcome to Http://beginer2cs.blogspot.com for crash course of php<br /><br />";  
  $i++; // increment operator equivalent to i=i+1  
  }  
 ?>  

OutPut is repeated String  "welcome to Http://beginer2cs.blogspot.com for crash course of php " and no of reputation depend on condition of While loop.

Related Post Easiest Way to Create Ad hoc Network (without any Software)

Do ..while loop. : The main difference b/w While & do..while loop is :
While loop first check condition then execute condition But Do .. while loop first execute code once than check condition . because of that it is confirm that  Do ..while loop must execute atleast once .

 <?php  
 //Looping do..While loop :  
 $i=0;  
 do  
 {  
  echo "The no is ".$i."<br /><br />";  
  $i++; // increment operator equivalent to i=i+1  
  }while($i<4)  
  // What is main different b/w while loop & do..while loop  
 ?>  

OutPut is a repeated string you can check.

For LOOP : The loop is usedwhen you know in advance how many time the script should Run.

for( initial cond ; end cond ; increment or decrements )
    {
        code to be execute;
     } 

 <?php  
 //Looping for loop :  
 for($i=5;$i<=11;$i++)  
 {  
  echo "The no is ".$i."<br /><br />";  
  }  
  // What is main different b/w while loop & do..while loop  
 ?>  

Output is also repeated string .

Related Post PHP crash course -1

There is another loop in PHP foreach it is used to loop through arrays ..
we will discuss this after arrays .

Next section Is all about array .

PHP crash course -4 ( GET POST & REQUEST function )

PHP crash course -1
PHP crash course -3 

No comments:

Post a Comment

THANKS FOR UR GREAT COMMENT

Blogger Widgets