Searching...
Friday 21 June 2013

PHP Basics – Part 8 :: Control Structures – Part 1


Control structures determine the flow of code within an application, defining execution characteristics such as whether and how many times a particular code statement will execute, as well as when a code block will relinquish execution control. These structures also offer a simple means to introduce entirely new sections of code (via file-inclusion statements) into a currently executing script. In this section, you’ll learn about all such control structures available to the PHP(php_official_download_website) language. Sit tight. This is the last topic of PHP Basics and this is quite important.

Conditional Statements

Conditional statements make it possible for your computer program to respond accordingly to a wide variety of inputs, using logic to discern between various conditions based on input value. This functionality is so basic to the creation of computer software that it shouldn’t come as a surprise that a variety of conditional statements are a staple of all mainstream programming languages, PHP included.

The if Statement

The if statement is one of the most commonplace constructs of any mainstream programming language, offering a convenient means for conditional code execution. The following is the syntax:

if (expression) {
statement
}

As an example, suppose you want a congratulatory message displayed if the user guesses a predetermined secret number:

<?php
$secretNumber = 453;
if ($_POST['guess'] == $secretNumber) {
echo "<p>Congratulations!</p>";
}
?>

The hopelessly lazy can forgo the use of brackets when the conditional body consists of only a single statement. Here’s a revision of the previous example:

<?php
$secretNumber = 453;
if ($_POST['guess'] == $secretNumber) echo "<p>Congratulations!</p>";
?>

Note:- Alternative enclosure syntax is available for the if, while, for, foreach, and switch control structures. This involves replacing the opening bracket with a colon (:) and replacing the closing bracket with endif;, endwhile;, endfor;, endforeach;, and endswitch;, respectively. There has been discussion regarding deprecating this syntax in a future release, although it is likely to remain valid for the foreseeable future. Better not to bother about these.

The else Statement

The problem with the previous example is that output is only offered for the user who correctly guesses the secret number. All other users are left destitute, completely snubbed for reasons presumably linked to their lack of psychic power. What if you want to provide a tailored response no matter the outcome? To do so you would need a way to handle those not meeting the if conditional requirements, a function handily offered by way of the else statement. Here’s a revision of the previous example, this time offering a response in both cases:

<?php
$secretNumber = 453;
if ($_POST['guess'] == $secretNumber) {
echo "<p>Congratulations!!</p>";
} else {
echo "<p>Sorry!</p>";
}
?>

Like if, the else statement brackets can be skipped if only a single code statement is enclosed.

The elseif Statement

The if-else combination works nicely in an “either-or” situation—that is, a situation in which only two possible outcomes are available. But what if several outcomes are possible? You would need a means for considering each possible outcome, which is accomplished with the elseif statement. Let’s revise the secret-number example again, this time offering a message if the user’s guess is relatively close (within ten) of the secret number:

<?php
$secretNumber = 453;
$_POST['guess'] = 442;
if ($_POST['guess'] == $secretNumber)
{
echo "<p>Congratulations!</p>";
}
elseif (abs ($_POST['guess'] - $secretNumber) < 10)
{
echo "<p>You're getting close!</p>";
}
else
{
echo "<p>Sorry!</p>";
}
?>

Like all conditionals, elseif supports the elimination of bracketing when only a single statement is enclosed.

The switch Statement

You can think of the switch statement as a variant of the if-else combination, often used when you need to compare a variable(more about variables) against a large number of values:

<?php
switch($category)
{
case "news":
echo "<p>What's happening around the world</p>";
break;
case "weather":
echo "<p>Your weekly forecast</p>";
break;
case "sports":
echo "<p>Latest sports highlights</p>";
break;
default:
echo "<p>Welcome to my web site</p>";
}
?>

Note:- The presence of the break statement at the conclusion of each case block. If a break statement isn’t present, all subsequent case blocks will execute until a break statement is located. 

As an illustration of this behavior, let’s assume that the break statements are removed from the preceding example and that $category is set to weather. You’d get the following results:

Your weekly forecast
Latest sports highlights
Welcome to my web site

and when the break statements are not removed and $category is set to weather. You’d get the following result:

Your weekly forecast

0 comments:

Post a Comment

 
Back to top!