Calling Functions


Functions come in two flavorsthose built in to the language and those you define yourself. PHP has hundreds of built-in functions. Take a look at the following snippet for an example of a function in use:

strtoupper("Hello Web!");


This example calls the strtoupper() function, passing it the string "Hello Web!". The function then goes about its business of changing the contents of the string to uppercase letters. A function call consists of the function name (strtoupper in this case) followed by parentheses. If you want to pass information to the function, you place it between these parentheses. A piece of information passed to a function in this way is called an argument. Some functions require that more than one argument be passed to them, separated by commas:

some_function($an_argument, $another_argument);


strtoupper() is typical for a function in that it returns a value. Most functions return some information back after they've completed their taskthey usually at least tell whether their mission was successful. strtoupper() returns a string value, so its usage requires the presence of a variable to accept the new string, such as

$new_string = strtoupper("Hello Web!");


You may now use $new_string in your code, such as to print it to the screen:

echo $new_string;


This code will result in the following text on the screen:

HELLO WEB!


By the Way

The print() and echo() functions are not actually functions, they're language constructs designed to output strings to the browser. However, you will find them in the PHP function list, at http://www.php.net/print and http://www.php.net/echo, respectively. These constructs are similar in functionality and can be used interchangably. Whichever one you use is a matter of taste.


The abs() function, for example, requires a signed numeric value and returns the absolute value of that number. Let's try it out in Listing 7.1.

Listing 7.1. Calling the Built-in abs() Function

 1: <?php 2: $num = -321; 3: $newnum = abs($num); 4: echo $newnum; 5: //prints "321" 6: ?>

In this example, we assign the value -321 to a variable $num. We then pass that variable to the abs() function, which makes the necessary calculation and returns a new value. We assign this to the variable $newnum and display the result.

Put these lines into a text file called abs.php and place this file in your web server document root. When you access this script through your web browser, it produces the following:

321


In fact, we could have dispensed with temporary variables altogether, passing our number straight to the abs() function and directly printing the result:

echo abs(-321);


We used the temporary variables $num and $newnum, though, to make each step of the process as clear as possible. Sometimes you can make your code more readable by breaking it up into a greater number of simple expressions.

You can call user-defined functions in exactly the same way that we have been calling built-in functions.




Sams Teach Yourself PHP, MySQL And Apache All in One
Sams Teach Yourself PHP, MySQL and Apache All in One (3rd Edition)
ISBN: 0672328739
EAN: 2147483647
Year: 2004
Pages: 327

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net