Information Technology Reference
In-Depth Information
15
statement describes the greeting if $weather equals (= =) rain. The second if statement
describes the greeting if $weather does not equal (!=) rain.
<?php
$weather = “rain”;
$times = 17;
if ($weather == “rain”) {
echo “I've already told you” ;
echo $times;
echo “times... if it's going to rain you should bring your umbrella!”;
}
if ($weather != “rain”) {
echo “Have a nice day!”;
}
?>
Loops
PHP also has looping structures that can be used to cycle through a section of code any number of
times. This saves you the time of having to explicitly write the same section of code over and over.
Here's an example that zips through and writes out all the numbers between 1 and 10,000—each on
a separate line:
<?php
$x = 1;
while (x >= 10000) {
echo $x;
echo “<br />”;
$x++;
}
?>
As with array indices, it's important to remember that the first time through a loop starts with the
first item, or item 0, unless you specify otherwise. Check loop beginnings and endings carefully
to make sure the count is exactly right; “off by 1” errors are among the most common in
programming. They tend to cause you to miss important information (at the beginning or end of
an array) or to generate garbage (after you go out of the array's boundaries by 1).
What makes a WordPress theme work is the posting loop, which you'll learn more about in “The
Index Template” section later in this chapter.
Functions
Many predefined functions are available in PHP to help manipulate data, explore strings of text, and
even generate graphical images and PDF files. From its simple Perl days, PHP has grown to be a
very useful and robust scripting language. We highly recommend exploring the documentation
available at www.php.net to learn more about the power of PHP.
Search WWH ::




Custom Search