27
PHP Language Operators & Control Structures

Php Operators N Controllers

Embed Size (px)

Citation preview

Page 1: Php Operators N Controllers

PHP Language

Operators & Control Structures

Page 2: Php Operators N Controllers

Switch-case() statement

Switch (decision-variable){

case first condition is true:do this!

case second condition is true:do this!

.

.

.… and so on …

}

Page 3: Php Operators N Controllers

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

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

case() conditions

Page 4: Php Operators N Controllers

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>

Page 5: Php Operators N Controllers

While() loop

While (condition is true)

{

do this!

}

Page 6: Php Operators N Controllers

• 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.

Page 7: Php Operators N Controllers

Examples

<?php

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

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

}

?>

Page 8: Php Operators N Controllers

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.

Page 9: Php Operators N Controllers

Output of example

Page 10: Php Operators N Controllers

• 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.

Page 11: Php Operators N Controllers

Do-while() statement

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

Page 12: Php Operators N Controllers

While() vs do-while()

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

Page 13: Php Operators N Controllers

Examples

• <?php

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

?>

Page 14: Php Operators N Controllers

• 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:

Page 15: Php Operators N Controllers

Example

• <?php

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

?>

Page 16: Php Operators N Controllers

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

Page 17: Php Operators N Controllers

• <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>

Page 18: Php Operators N Controllers

• 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.

Page 19: Php Operators N Controllers

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...

Page 20: Php Operators N Controllers

• The for() loop typically looks like this:

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

Page 21: Php Operators N Controllers

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>

Page 22: Php Operators N Controllers

• 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.

Page 23: Php Operators N Controllers

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.

Page 24: Php Operators N Controllers

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.

Page 25: Php Operators N Controllers

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.

Page 26: Php Operators N Controllers

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.

Page 27: Php Operators N Controllers

• <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>