User-Defined Functions

only for RuBoard - do not distribute or recompile

User -Defined Functions

You can define a function to be called later. You can put the function in a file to be included in different Web pages. Functions you define are used just like library functions. You must define a function before you call it.

Functions can return values. If you use a return statement, the value after the return replaces the function call in a statement. For example:

 function myfunc(){   return 4; } $a = 1 + myfunc(); printf("a = %d\n",$a);//prints a = 5 

Functions can be called with arguments. Arguments are lists of variables that can be used by the code within the function. Arguments can be passed by value or by reference. (For a detailed description of by reference/by value, see the Reference section under Operators in this Appendix.) You can define default values for function arguments. The following code assigns a default value to the argument in the function get_color():

 function get_color($coat_length = "short") {     if ($coat_length == "short")         return "brown";     else         return "white";     } printf ("the short coat color is %s\n",get_color());                               /* prints "the short coat color is brown */ printf("the long coat color is %s\n",get_color("long"));                               /* prints the long coat color is white */ 

You must put default arguments on the right side of non-default arguments. The following shows the correct way and the incorrect way to assign default arguments:

 function foo($arg1, $arg2 = "defaulted") {} // the right way function foobad ($arg1 = "defaulted", $arg2 {} // the wrong way 

In the preceding code, if foo is called with one argument; foo("hello"), then PHP is happy. If foobad is called with one argument; foobad("hello"), then you will get a warning similar to this:

 Warning: Missing argument 2 in call to foobad() in ;/home/httpd/html/test/test.php3 on line 4 

Some of PHP's library functions require a user-written callback function as one of the parameters. For example, the usort() function sorts an array. It requires you to give it the name of a comparison function, as shown:

 Function cmp($a,$b) { if ($a>$b)    return 1;  if ($a<$b)    return 1;  return 0; // must be equal } $arr[0]=5; $arr[1]=2; $arr[3]=4; $arr[4]=1;   usort($arr,"cmp"); 

In the current version of PHP 4, you can also pass an array containing an object and a method, as shown:

 Class sort {   Var $misc;   Function compare($a,$b)     {     if ($a>$b)         return 1;      if ($a<$b)        return 1;     return 0; // must be equal     }  }  $MySort = new sort; $arr[0]=5; $arr[1]=2; $arr[3]=4; $arr[4]=1;   usort($arr,array($MySort,"compare"); 

An old_func keyword has been deprecated. It should not be used. It allows you to define functions using the old PHP/FI2 syntax. If you do this, PHP3 has difficulty using the function thus defined, and the function should never be used in PHP 4. There is no reason to use that function. If you need to use old PHP2 code, use the PHP/FI2 -> PHP3 convertor.

only for RuBoard - do not distribute or recompile


MySQL and PHP From Scratch
MySQL & PHP From Scratch
ISBN: 0789724405
EAN: 2147483647
Year: 1999
Pages: 93
Authors: Wade Maxfield

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