Searching...
Sunday 23 June 2013

PHP Basics – Part 8 :: Control Structures - Part 3


The foreach Statement

The foreach looping construct syntax is adept at looping through arrays, pulling each key/value pair from the array until all items have been retrieved or some other internal conditional has been met. Two syntax variations are available, each of which is introduced with an example.

The first syntax variant strips each value from the array, moving the pointer closer to the end with each iteration. The following is its syntax:

foreach (array_expr as $value) {
statement
}


Suppose you want to output an array of links, like so:

<?php
$links = array("www.apress.com","www.php.net","www.apache.org");
echo "<b>Online Resources</b>:<br />";
foreach($links as $link)
{
echo "<a href=\"http://$link\">$link</a><br />";
}
?>
This would result in the following:

In browser source
Online Resources:<br />
<a href="http://www.apress.com">http://www.apress.com</a><br />
<a href="http://www.php.net">http://www.php.net</a><br />
<a href="http://www.apache.org">http://www.apache.org</a><br />

In browser window

The second variation is well-suited for working with both the key and value of an array. The syntax follows:

foreach (array_expr as $key => $value) {
statement
}


Revising the previous example, suppose that the $links array contains both a link and a corresponding link title:

$links = array("The Apache Web Server" => "www.apache.org", "Apress" => "www.apress.com", "The PHP Scripting Language" => “www.php.net”);

Each array item consists of both a key and a corresponding value. The foreach statement can easily peel each key/value pair from the array, like this:

echo "<b>Online Resources</b>:<br />";
foreach($links as $title => $link)
{
echo "<a href=\"http://$link\">$title</a><br />";
}

The result would be that each link is embedded under its respective title, like this:

In browser source
Online Resources:<br />
<a href="http://www.apache.org”>The Apache Web Server</a><br />
<a href="http://www.apress.com">Apress</a><br />
<a href="http://www.php.net">The PHP Scripting Language</a><br />

In browser window

The break and goto Statements

Encountering a break statement will immediately end execution of a do...while, for, foreach, switch, or while block. For example, the following for loop will terminate if a prime number is pseudo-randomly happened upon:

<?php
$primes = array(2,3,5,7,11,13,17,19,23,29,31,37,41,43,47);
for($count = 1; $count++; $count < 1000)
{
$randomNumber = rand(1,50);
if (in_array($randomNumber,$primes))
{
break;
} else
{
printf("Non-prime number found: %d <br />", $randomNumber);
}
}
?>

Sample output follows:

Non-prime number found: 48
Non-prime number found: 42
Prime number found: 17

Through the addition of the goto statement in PHP 5.3, the break feature was extended to support labels. This means you can suddenly jump to a specific location outside of a looping or conditional construct. An example follows:

<?php
for ($count = 0; $count < 10; $count++)
{
$randomNumber = rand(1,50);
if ($randomNumber < 10)
goto less;
else
echo "Number greater than 10: $randomNumber<br />";
}
less:
echo "Number less than 10: $randomNumber<br />";
?>

It produces the following (your output will vary):

Number greater than 10: 22
Number greater than 10: 21

The continue Statement

The continue statement causes execution of the current loop iteration to end and commence at the beginning of the next iteration. For example, execution of the following while body will recommence if $usernames[$x] is found to have the value missing:

<?php
$usernames = array("Grace","Doris","Gary","Nate","missing","Tom");
for ($x=0; $x < count($usernames); $x++)
{
if ($usernames[$x] == "missing") continue;
printf("Staff member: %s <br />", $usernames[$x]);
}
?>

This results in the following output:

Staff member: Grace
Staff member: Doris
Staff member: Gary
Staff member: Nate
Staff member: Tom

0 comments:

Post a Comment

 
Back to top!