WWW PHP 2003 1 2
function function_name (parameter 1, parameter 2, parameter n ) statement list function_name sin, Sin, SIN parameter 1, parameter 2, parameter n 0 1 1 PHP HTML 3 function strcat ($left, $right) $combined_string = $left. $right; return $combined_string; function strcat ($left, $right) return $left. $right; <?php function col ()?> </td><td> <??> col() </td><td> HTML 4
PHP4 <?php $makefoo = true; /* We can't call foo() from here since it doesn't exist yet, but we can call bar() */ bar(); if ($makefoo) function foo () echo "I don't exist until program execution reaches me.\n"; 5 /* Now we can safely call foo() since $makefoo evaluated to true */ if ($makefoo) foo(); function bar() echo "I exist immediately upon program start.\n";?> 6
PHP4 A B B A function foo() function bar() echo "I don't exist until foo() is called.\n"; /* We can't call bar() yet since it doesn't exist. */ foo(); /* Now we can call bar(), foo()'s processing has made it accessible. */ bar(); 7 function_name (argument_list); $result = function_name (argument_list); $result function_name argument_list 8
function strcat ($left, $right) return $left. $right; // strcat Hello, woild! $msg = strcat ( Hello,, woild! ); // $msg Hello, world! 9 Pass-by-value References Pass-by-reference 10
Pass-by-value $a = 3; $b = 4; $m = max($a, $b); max ($x, $y) return ($x >= $y)? $x : $y; $a 3 $b 4 3 $x max 4 $y 11 $a = 3; $b = 4; swap($a, $b); swap ($x, $y) $temp = $x; $x = $y; $y = $temp; $a 3 $b 4 3 $x swap 4 $y swap 12 $x $y a b
References PHP 3 $foo name content $foo 3 $bar = & $foo; $foo $bar $foo 3 $bar 13 Pass-by-reference & function inc (& $x) $x++; $a = 10; inc($a); echo $a; // 11 function inc ($x) $x++; $a = 10; inc($a); echo $a; // 10 14
$a = 3; $b = 4; swap($a, $b); swap (&$x, &$y) $temp = $x; $x = $y; $y = $temp; $a $b $x $y swap swap x y a b 15 function makecoffee ($type = "cappuccino ) return "Making a cup of $type.\n"; echo makecoffee (); // Making a cup of cappuccino echo makecoffee ("espresso"); // Making a cup of espresso 16
func_num_args ( ) : func_get_arg ( $n ) : $n func_get_args ( ) function foo() $numargs = func_num_args(); echo "Number of arguments: $numargs<br>\n"; if ($numargs >= 2) echo "Second argument is: ". func_get_arg (1). "<br>\n"; $arg_list = func_get_args(); for ($i = 0; $i < $numargs; $i++) echo "Argument $i is: ". $arg_list[$i]. "<br>\n"; foo (1, 2, 3); 17 PHP $a = 3; // $a is a global variable. function foo () $a += 100; // $a is a local variable. foo(); echo $a; // print global $a, i.e., 3. 18
global $a = 1; $b = 2; function Sum() global $a, $b; $a = 1; $b = 2; function Sum() // global $a, $b; $b = $a + $b; $b = $a + $b; Sum(); echo $b; // 3 Sum(); echo $b; // 2 19 superglobal global PHP $GLOBALS Contains a reference to every variable which is currently available within the global scope of the script. The keys of this array are the names of the global variables. $_SERVER Variables set by the web server or otherwise directly related to the execution environment of the current script. $_GET Variables provided to the script via HTTP GET. 20
$_POST Variables provided to the script via HTTP POST. $_COOKIE Variables provided to the script via HTTP cookies. $_FILES Variables provided to the script via HTTP post file uploads. $_ENV Variables provided to the script via the environment. $_REQUEST Variables provided to the script via any user input mechanism, and which therefore cannot be trusted.. $_SESSION Variables which are currently registered to a script's session. 21 function test_global() // Most predefined variables aren't "super" and require // 'global' to be available to the functions local scope. global $HTTP_POST_VARS; echo $HTTP_POST_VARS['name']; // Superglobals are available in any scope and do // not require 'global'. Superglobals are available // as of PHP 4.1.0 echo $_POST['name']; 22
static variable function counter () static $count = 0; return $count++; for ($i = 1; $i <= 5; $i++) echo counter(); function counter () $count = 0; return $count++; for ($i = 1; $i <= 5; $i++) echo counter(); 0 1 2 3 4 0 0 0 0 0 23 return function return_one () return 100; function return_two () return array( Fred, 35); 24
Variable Functions () PHP function foo() echo "In foo()<br>\n"; function bar($arg = ' ') echo "In bar(); argument was '$arg'.<br>\n"; PHP echo(), print(), unset(), isset(), empty(), include(), require() $func = 'foo'; $func(); // This calls foo() $func = 'bar'; $func('test'); // This calls bar() 25 Anonymous Functions PHP create_function ($args, $code ) $newfunc = create_function ( '$a, $b', 'return "ln($a) + ln($b) = ".log($a * $b); ); echo "New anonymous function: $newfunc\n"; echo $newfunc(2,m_e)."\n"; // outputs // New anonymous function: lambda_1 // ln(2) + ln(2.718281828459) = 1.6931471805599 26