Creating and Calling Functions that Take Arguments

I l @ ve RuBoard

Although being able to create a simple function is useful, writing one that takes input and does something with that input is even better. The input a function takes is called an argument and there are no limits to how many arguments a function can take. Functions that take arguments is a concept you've seen before: the print() function takes a string as an argument which it then sends to the browser.

The syntax for writing functions that take arguments is as follows :

 function FunctionName ($Argument1, $Argument2, etc.) {    statement(s); } 
Script 9.3. The existing hello.php page creates a customized greeting over several lines of code within the rest of the page.

graphics/09sc03.gif

These arguments will be in the form of variables which get assigned the value sent to the function when you call it. Functions that take input are called much like those which do not, you just need to remember to pass along the necessary values. You can do this either by passing variables :

 FunctionName ($Variable1, $Variable2,   etc.); 

or by placing values within quotes, as in

 FunctionName ("Value1", "Value2", etc.); 

or some combination thereof:

 FunctionName ($Variable1, "Value2",   etc.); 

The important thing to note is that arguments are passed quite literally in that the first value in the function will be equal to the first value in the call line, the second function value matches the second call value, and so forth. Functions are not smart enough to intuitively understand how you meant the values to be associated. This is also true if you fail to pass a value, in which case the function will assume that value is null ( null is not the mathematical 0, which is actually a value, but closer to the idea of the word nothing ). The same thing applies if a function takes four arguments and you pass threethe fourth will be null.

To demonstrate functions that take arguments, you'll rewrite the hello.php page from Chapter 6, Control Structures . You'll place the greeting code into a simple function that takes an argumentthe user 's name .

To create and call a function that takes an argument:

  1. Open hello.php in your text editor (Script 9.3).

  2. Begin with the opening PHP tag, <?php, then take lines 7-16 from the original script (Script 9.3) and place them within a function at the beginning of the page (Script 9.4).

    Script 9.4. Here is a function that takes one argument which must be passed to it when the function is called. To insure that the function works properly, it is only called if $Username has a value.

    graphics/09sc04.jpg

     function GreetUser ($TheUser) { print ("Good ");    if (date("A") == "AM") {       print ("morning, ");    } elseif ((date("H")>12) and (date("H")<18)) { print ("morning, ");    } else {       print ("evening, ");    } // Close our date if.    print ("$TheUser");    print ("!\n"); } // End of GreetUser function. 

    After you give the function an appropriate name, the function itself consists of the same lines used before. The $TheUser variable will receive the value of $Username sent to the function once it is called (see line 21 of Script 9.4).

  3. Close the initial PHP section and create the HTML header.

     ?><HTML><HEAD><TITLE>The GreetUser Function</TITLE><BODY> 
  4. Reopen a PHP section and place the if-else conditional there, including a call to the new function.

     <?php if ($Username) {    GreetUser ($Username); // Call the function. } else {    print ("Please log in.\n"); } // Close our username if. 

    You should keep the if ($Username) conditional here so that the function is only called if the $Username exists.

  5. Close the PHP and HTML.

     ?></BODY></HTML> 
  6. Save your script, upload it to the server, and test it in your Web browser (Figure 9.2). Remember to send the script a Username valueby appending it to the URL or via an HTML formor else you'll get a result like that in Figure 9.3.

    Figure 9.2. Now that you've created a function for making a time-specific welcome, even though it may appear similar to our original hello.php page, this greeting can quickly be generated multiple times without rewriting any code.

    graphics/09fig02.gif

    Figure 9.3. As you learn more and more advanced things about PHP programming, do not forget the simple facts. Here the omission of sending hello.php a Username value (compare with the URL in Figure 9.2) generates this message, as you prudently programmed it to do.

    graphics/09fig03.jpg

Tip

In Chapter 13, Creating Web Applications , you'll see how to place certain information into external files which can be used in several pages. An external file will often be the best place to put your own functions, such as GreetUser, so they will be universally accessible throughout the entire Web site.


Tip

Technically if you pass a number as an argument in a function, it does not need to be within strings but there is no harm in using strings anyway to remain consistent as to how arguments are passed.


I l @ ve RuBoard


PHP for the World Wide Web (Visual QuickStart Guide)
PHP for the World Wide Web (Visual QuickStart Guide)
ISBN: 0201727870
EAN: 2147483647
Year: 2001
Pages: 116
Authors: Larry Ullman

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