Online Book Reader

Home Category

HTML, XHTML and CSS All-In-One for Dummies - Andy Harris [222]

By Root 1428 0
either the at sign or the period. Remember to specify the period with \. because an ordinary period means “any character.”

preg_split works well for timestamps, e-mail addresses, and other things where there’s more than just one unique delimiter that you wish to split the string on.

Figure 4-9: The e-mail address split into an array.

Earlier versions of PHP had a function called split. It was much like the preg_split function, but it used a different regular expression syntax. Hardly anybody used it, and it will not be in PHP6 and later. Use explode for simple patterns and preg_split when you need the power of regular expressions.

Chapter 5: Using Functions and Session Variables

In This Chapter

Creating functions to manage your code’s complexity

Enhancing your code by using functions

Working with variable scope

Getting familiar with session variables

Incorporating session variables into your code


PHP programs are used to solve interesting problems, which can get quite complex. In this chapter, you explore ways to manage this complexity. You discover how to build functions to encapsulate your code. You also learn how to use session variables to make your programs keep track of their values, even when the program is called many times.


Creating Your Own Functions

It won’t take long before your code starts to get complex. Functions are used to manage this complexity. As an example, take a look at Figure 5-1.


Rolling dice the old-fashioned way

Before I show you how to improve your code with functions, look at a program that doesn’t use functions so you have something to compare with.

The following rollDice.php program creates five random numbers and displays a graphic for each die:

“http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>

rollDice1.php

RollDice 1

Uses Sequential Programming

$roll = rand(1,6);

$image = ”die$roll.jpg”;

print <<< HERE

alt = ”roll: $roll” />

HERE;

$roll = rand(1,6);

$image = ”die$roll.jpg”;

print <<< HERE

alt = ”roll: $roll” />

HERE;

$roll = rand(1,6);

$image = ”die$roll.jpg”;

print <<< HERE

alt = ”roll: $roll” />

HERE;

$roll = rand(1,6);

$image = ”die$roll.jpg”;

print <<< HERE

alt = ”roll: $roll” />

HERE;

$roll = rand(1,6);

$image = ”die$roll.jpg”;

print <<< HERE

alt = ”roll: $roll” />

HERE;

?>

Here are some interesting features of this code:

♦ The built-in rand() function rolls a random number. Whenever possible, try to find functions that can help you. The rand() function produces a random integer. If you use two parameters, the resulting number will be in the given range. To roll a standard six-sided die, use rand(1,6):

$roll = rand(1,6);

♦ I created an image for each possible roll. To make this program more visually appealing, I created an image for each possible die roll. The images are called die1.jpg, die2.jpg, and so on. All these images are stored in the same directory as the PHP program.

♦ The img tag is created based on the die roll. After I have a die roll, it’s easy to create an image based on that roll:

$image = “die$roll.jpg”;

print <<< HERE

alt = “roll: $roll” />

HERE;

♦ The die-rolling code is repeated five times. If you can roll one die, you can easily roll five. It’s as easy as copying and pasting the code. This seems pretty easy, but it leads to problems. What if I want to change the way I roll the dice? If so, I’ll have to change the code five times. What if I want to roll 100 dice? The program will quickly become unwieldy. In general, if you find yourself copying

Return Main Page Previous Page Next Page

®Online Book Reader