Online Book Reader

Home Category

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

By Root 1471 0
and pasting code, you can improve the code by adding a function.

Figure 5-1: This program rolls five dice.

Improving code with functions

Functions are predefined code fragments. After you define a function, you can use it as many times as you wish. As you can see in the following code, the outward appearance of this program is identical to rollDice1.php, but the internal organization is quite different:

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

rollDice2.php

RollDice 2

Uses Functions

function rollDie(){

$roll = rand(1,6);

$image = ”die$roll.jpg”;

print <<< HERE

alt = ”roll: $roll” />

HERE;

} // end rollDie

for ($i = 0; $i < 5; $i++){

rollDie();

} // end for loop

?>

Here’s how things have changed in this version:

1. Use the function keyword to define a function.

The function keyword indicates that a function definition will follow. The code inside the definition won’t be run immediately, but instead, PHP will “remember” the code inside the function definition and play it back on demand:

function rollDie(){

2. Give the function a name.

The function name should indicate what the function does. I call my function rollDie() because that’s what it does (rolls a die):

function rollDie(){

3. Specify arguments with parentheses.

You can send arguments (special variables for your function to work with) by indicating them in the parentheses. This function doesn’t need arguments, so I leave the parentheses empty:

function rollDie(){

For more information on functions, arguments, and the return statement, turn to Book IV, Chapter 4. Functions in PHP act almost exactly like their cousins in JavaScript.

4. Begin the function definition with a left brace ({).

The left brace is used to indicate the beginning of the function code.

5. Indent the code that makes up your function.

Use indentation to indicate which code is part of your function. In this case, the function generates the random number and prints an image tag based on that random number:

function rollDie(){

$roll = rand(1,6);

$image = “die$roll.jpg”;

print <<< HERE

alt = “roll: $roll” />

HERE;

} // end rollDie

6. Denote the end of the function with a right brace (}).

7. Call the function by referring to it.

After the function is defined, you can use it in your code as if it were built into PHP. In this example, I call the function inside a loop:

for ($i = 0; $i < 5; $i++){

rollDie();

} // end for loop

Because the code is defined in a function, it’s a simple matter to run it as many times as I want. Functions also make your code easier to read because the details of rolling the dice are hidden in the function.


Managing variable scope

Two kinds of scope are in PHP: global and local.

If you define a variable outside a function, it has the potential to be used inside any function. If you define a variable inside a function, you can access it only from inside the function in which it was created. See Book IV, Chapter 4 for more on variable scope.

Naming functions and variables

It can be hard to come up with a good naming scheme for your variables and functions. Doing so is very important because when you come back to your program, if you haven’t named your functions and variables consistently, you’ll have a hard time understanding what you wrote. Here are two common naming schemes to make this simple: using underscores (_) between words or camel-casing.

Using underscores is as straightforward as separating_each_word_with_an_underscore. It’s readable, but it’s ugly and can cause the variable names to get awfully lengthy.

The method I prefer and use throughout this book is camel-casing,

Return Main Page Previous Page Next Page

®Online Book Reader