Php Operators N Controllers

Preview:

Citation preview

PHP Language

Operators & Control Structures

Switch-case() statement

Switch (decision-variable){

case first condition is true:do this!

case second condition is true:do this!

.

.

.… and so on …

}

• Appropriate case() block execution– Depend on the value of decision variable

• Default block– Decision variable not match any of the listed

case() conditions

Example• <?php

// get form selection$day = $_GET['day'];// check value and select appropriate itemswitch ($day) {    case 1:        $special = 'Chicken in oyster sauce';        break;    case 2:        $special = 'French onion soup';        break;    case 3:        $special = 'Pork chops with mashed potatoes and green salad';        break;    default:        $special = 'Fish and chips';        break;}

?>

<h2>Today's special is:</h2><?php echo $special ?></body></html>

While() loop

While (condition is true)

{

do this!

}

• Execution of php statements within curly braces– As long as the specified condition is true

• Condition becomes false– The loop will be broken and the following

statement will be executed.

Examples

<?php

$number = 5;while ($number >=2)

{echo $number. "<br/>" ;$number - = 1;

}

?>

Examples

• Variable is initialized to 5

• The while loop executes as long as the condition, ($number>=2) is true

• At the end of the loop block, the value of $number is decreased by 1.

Output of example

• The while() loop executes a set of statements while a specified condition is true. But what happens if the condition is true on the first iteration of the loop itself? If you're in a situation where you need to execute a set of statements *at least* once, PHP offers you the do-while() loop.

Do-while() statement

• do {    do this!} while (condition is true)

While() vs do-while()

• Let's take a quick example to better understand the difference between while() and do-while():

Examples

• <?php

$x = 100;// while loopwhile ($x == 700) {    echo "Running...";    break;}

?>

• In this case, no matter how many times you run this PHP script, you will get no output at all, since the value of $x is not equal to 700. But, if you ran this version of the script:

Example

• <?php

$x = 100;// do-while loopdo {    echo "Running...";    break;} while ($x == 700);

?>

• you would see one line of output, as the code within the do() block would run once.

• <html><head></head><body>

<?php

// set variables from form input$upperLimit = $_POST['limit'];$lowerLimit = 1;// keep printing squares until lower limit = upper limitdo {    echo ($lowerLimit * $lowerLimit).'&nbsp;';    $lowerLimit++;} while ($lowerLimit <= $upperLimit);// print end markerecho ' END';

?>

</body></html>

• Thus, the construction of the do-while() loop is such that the statements within the loop are executed first, and the condition to be tested is checked afterwards. This implies that the statements within the curly braces would be executed at least once.

For() loops

• Both the while() and do-while() loops continue to iterate for as long as the specified conditional expression remains true. But what if you need to execute a certain set of statements a specific number of times - for example, printing a series of thirteen sequential numbers, or repeating a particular set of <td> cells five times? In such cases, clever programmers reach for the for() loop...

• The for() loop typically looks like this:

•for (initial value of counter; condition; new value of counter) {    do this!}

Examples• <html>

<head><basefont face="Arial"></head><body>

<?php

// define the number$number = 13;// use a for loop to calculate tables for that numberfor ($x = 1; $x <= 10; $x++) {    echo "$number x $x = ".($number * $x)."<br />";}

?>

</body></html>

• define the number to be used for the multiplication table.

• constructed a for() loop with $x as the counter variable, initialized it to 1.

• specified that the loop should run no more than 10 times.

• The auto-increment operator automatically increments the counter by 1 every time the loop is executed. Within the loop, the counter is multiplied by the number,

• to create the multiplication table, and echo() is used to display the result on the page.

Summary

• It is often desirable when writing code to perform different actions based on different decision.

• In addition to the if Statements, PHP includes a fourth

type of conditional statement called the switch statement. The switch Statement is very similar or an alternative to the if...else if...else commands. The switch statement will test a condition. The outcome of that test will decide which case to execute. Switch is normally used when you are finding an exact (equal) result instead of a greater or less than result. When testing a range of values, the if statement should be used.

Summary

• In programming it is often necessary to repeat the same block of code a number of times. This can be accomplished using looping statements. PHP includes several types of looping statements.

• The while statement loops through a block of code as long as a specified condition is evaluated as true. In other words, the while statement will execute a block of code if and as long as a condition is true.

Summary

• The do...while statement loops through a block of code as long as a specified condition is evaluated as true. In other words, the while statement will execute a block of code if and as long as a condition is true.

• The do...while loop is similar in nature to the While loop. The key difference is that the do...while loop body is guaranteed to run at least once. This is possible because the condition statement is evaluated at the end of the loop statement after the loop body is performed.

Summary

• The for statement loop is used when you know how many times you want to execute a statement or a list of statements. For this reason, the for loop is known as a definite loop.

• <html><head></head><body>

<?php

// get form selection$day = $_GET['day'];// check value and select appropriate itemswitch ($day) {    case 1:        $special = 'Chicken in oyster sauce';        break;    case 2:        $special = 'French onion soup';        break;    case 3:        $special = 'Pork chops with mashed potatoes and green salad';        break;    default:        $special = 'Fish and chips';        break;}

?>

<h2>Today's special is:</h2><?php echo $special ?></body></html>

Recommended