Saturday, July 8, 2017

PHP Control Instruction

PHP Control Instruction

Content

* Conditional Statement
* Loop

Conditional Statements

* Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do this
* we have the following conditional statements:

 - if statement - executes some code only if a specified condition is true
 - if...else statement - executes some code if a condition is true and another code if the condition is false
 - if...elseif....else statement - selects one of several blocks of code to be executed
 - switch statement - selects one of many blocks of code to be executed

if statement

if(some condition)
{
 /* code to be executed if condition is true */
}

Example

<html>
<body>
<?php
$t=date("H");
if ($t<"12"&&$t>"5") {
   echo "Good Morning!";
}
?>
</body>
</html>

if-else statement

if(some condition)
{
/* code to be executed if condition is true */
}
else
{
/* code to be executed if condition is false */
}

Example

<html>
<body>
<?php
$x=25;
if ($x%2==0) {
   echo "Even Number";
}
else{
   echo "Odd Number";
}
?>
</body>
</html>

if-else-if….else statement

if(some condition){
/* code to be executed if condition is true */
} else if (some condition){
/* code to be executed if condition is true */
}else if(some condition){
/* code to be executed if condition is true */
}
else {
/* code to be executed if condition is false */
}

Example

<html>
<body>
<?php
$t=date("H");
if ($t<"12") {
   echo "Good Morning";
}else if($t<"17"){
   echo "Good After Noon";
} else if($t<"10"){
   echo "Good After Noon";
}else{
   echo "Good Night";
}
?> 
</body>
</html>

switch statement

switch(n)
{
 case label1:
/* code to be executed if n==label1 */
case label2:
/* code to be executed if n==label2 */
case label3:
/* code to be executed if n==label3 */
case label4:
/* code to be executed if n==label4 */
....
default:
/* code to be executed if n is different from all labels */
}

Example

<html>
<body>
<?php
$country="INDIA";
switch($country){
case "INDIA":
echo "Rupee";
break;
case "USA":
echo "Dollar";
break;
case "JAPAN":
echo "Yen";
break;
case "ENGLAND":
echo "Pound";
break;
default:
 echo "No Currency";
}
?>
</body>
</html>

Loop

* Sometimes, you want the same block of code to run over and over again, we can use loops to perform a task like this
* In PHP, we have the following looping statements:
 - while
 - do while
 - for
 - foreach

while statement

* The while loop executes a block of code as long as the specified condition is true

while(condition)
{
/*Code to be executed*/
}

Example

<html>
<body>
<?php
$x=1;
while($x<=10){
echo $x;
$x++;
}
?>
</body>
</html>

do while statement

* The do...while loop will always execute the block of code once, it will then check the condition, and repeat the loop while the specified condition is true

do
{
/*Code to be executed*/
} while(condition);

Example

<html>
<body>
<?php
$x=1;
do
{
echo $x;
$x++;
} while($x<=10);
?>
</body>
</html>

* Notice that in a do while loop the condition is tested AFTER executing the statements within the loop. This means that the do while loop would execute its statements at least once, even if the condition fails the first time.
for  statement
* The for loop is used when you know in advance how many times the script should run.

for(initialization; condition; flow)
{
/*Code to be executed*/
}

* Initialization is assigning initial value to the counter variable. Condition is any expression which will be evaluated as true of false. Flow is any change in counter variable so that loop reaches to its termination.

Example

<html>
<body>
<?php
for($x=1;$x<=10;$x++)
{
echo $x;
}
?>
</body>
</html>



Note: foreach loop will be covered with Arrays.

<<Prev     Home     Next>>

No comments:

Post a Comment