Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
PHP Programming With MySQL Second Edition.doc
Скачиваний:
0
Добавлен:
01.05.2025
Размер:
43.07 Mб
Скачать

Ing example,

functions

must be

contained within

<?php . . . ?> tags,

like all PHP code.

Parameters are placed within the parentheses that follow the func-

tion name. A formal parameter, or simply a parameter, is a vari-

able that is passed to a function when it is called. To declare a

parameter, you only need to place the parameter name within the

parentheses of a function definition. In other words, you do not

need to explicitly declare and initialize a parameter as you do a

regular variable. For example, suppose you want to write a func-

tion named calculateSalesTotal() that calculates the sales

total of a number contained in a parameter named $Subtotal

for an online transaction. The function name would be written as


CHAPTER 2

Functions do

not have to

contain

parameters.

Many func-

tions only perform a task

and do not require exter-

nal data. For example,

you might create a func-

tion that displays the

same message each time

a user visits your Web

site; this type of function

only needs to be exe-

cuted and does not

require any other

information.

Functions and Control Structures

calculateSalesTotal($Subtotal). In this case, the function declara-

76

tion is declaring a new formal parameter (which is a variable) named

$Subtotal. Functions can contain multiple parameters separated

by commas. To declare three separate number parameters in the

calculateSalesTotal() function, you might write the function name

as calculateSalesTotal($Subtotal, $SalesTax, $Shipping).

Note that parameters such as $Subtotal, $SalesTax, and

$Shipping receive their values when you call the function from

elsewhere in your program. You can also assign default values to a

parameter as follows:

function sampleFunction($Num1="100", $Num2="200",

$Num3="300") {

echo ("<p>$Num1</p>");

echo ("<p>$Num2</p>");

echo ("<p>$Num3</p>");

}

Following the parentheses that contain the function parameters is a

set of curly braces (called function braces) that contain the function

statements. Function statements do the actual work of the function

(such as calculating the sales total), and must be contained within

the function braces. The following example of a function displays the

names of multiple companies:

function displayCompanyName($Company1, $Company2,

$Company3) {

echo "<p>$Company1</p>";

echo "<p>$Company2</p>";

echo "<p>$Company3</p>";

}

Notice how the preceding function is structured. The opening

curly brace is on the same line as the function name, and the clos-

Ing curly brace is on its own line following the function statements.

Each statement between the curly braces is indented five character

spaces. This structure is one standard format used by PHP program-

mers. However, other formats are used; many originated with other

programming languages and were carried forward to use with PHP.

Remember that tabs, spaces, and line breaks are included to help the

programmer and are ignored by the PHP scripting engine. For simple

functions, it is often easier to include the function name, curly braces,

and statements on the same line. For example, the following simpli-

fied version of the displayCompanyName() function is declared on a

single line:

function displayCompanyName($Company) {

echo "<p>$Company</p>"; }


Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]