Studyon Minte9.com
ZCE 5.3

Study

Functions



	// Function always return value (NULL, if not specified)
	// Only variable references should be returned by reference / &func() {return "abc";} // error
	// Scope - global, function, class
	// func_get_arg (for variable-length arguments)
	// Function value return by reference - &func()
	// Function arguments passed by reference - func(&a, &b=null)
	// Anonymous function - $a = function($var) {}; (Be aware of the last ;)
        // Lambda style function $str = create_function('$a,$b', $code.";")

* Basic syntax

Function name is not case-sensitive and must not start with a number.

<?php function name() {} function Name() {} // Fatal error: Cannot redeclare Name()
  * Returning Values Note that since return is a language construct and not a function, the parentheses surrounding its arguments are not required. It is common to leave them out, and you actually should do so as PHP has less work to do in this case.
<?php function A() { return (1); } function B() { return 2; // faster } echo A(); // Output: 1 echo B(); // Output: 2
  Even if you don't return a value, PHP will still cause your function to return NULL. The concept of "void" functions does not really apply to PHP.
<?php function name() {} echo name() === NULL; // Outputs: 1
  *** Return by reference Functions can also be declared so that they return by reference. Typically, this is used for things like resources (like database connections) and when implementing the Factory pattern. In this case you must return a variable.
<?php error_reporting(E_ALL); function &getHello() { return "Hello World"; } echo getHello(); // Notice: Only variable references should be returned by reference function &getHello2() { $a = "Hello World"; return $a; } echo getHello2(); // ok function &query($sql) { $result = mysql_query($sql); return $result; } echo query("SELECT 1 FROM mytable"); // ok
  * Variable Scope PHP has three variable scopes: the global scope, function scope, and class scope. * Variable-length Argument Lists You can create a function that accepts a variable number of arguments, depending on the circumstance. A typical example of this behaviour is exhibited by the printf() family of functions.
<?php echo sprintf("%s %s is %d old", "John", "Smith", 10); // Output: John Smith is 10 old
  PHP provides three built-in functions to handle variable-length argument lists: func_num_args(), func_get_arg(), func_get_args()
<?php function hello() { echo func_num_args() > 0 ? "Hello " . func_get_arg(0) : "Hello World"; } hello(); // Ouput: Hello World hello("John"); // Output: Hello John
  It is rather important to keep in mind that variable-length argument lists are full of potential pitfalls; while they are very powerful, they do tend to make your code confusing. * Passing Arguments by Reference Function arguments can also be passed by reference, as opposed to the traditional by-value method, by prefixing them with the by-reference operator &
<?php function countAll(&$count) { if (func_num_args() == 0) { die("You didn't specify at least one argument"); } else { $args = func_get_args(); // array of arguments array_shift($args); // remove first elemenent foreach($args as $v) { $count += strlen($v); } } } $count = 0; countAll($count, 'bar', 'baz', 'foo'); echo $count; // Output: 9
  Unlike PHP 4, PHP 5 allows default values to be specified for parameters even when they are declared as by-reference:
<?php function test($var1, &$var2 = null) { $var2 = "c"; } $var1 = 'a'; $var2 = 'b'; test($var1, $var2); echo $var2; // Output: c
  * Anonymous functions Anonymous functions, also known as closures, allow the creation of functions which have no specified name.
<?php preg_match("/-([a-z])/", "hello-world", $match); echo $match[1]; // output w echo preg_replace_callback('/-([a-z])/', function($match) { return strtoupper($match[1]); }, 'hello-world'); // output helloWorld
  Be aware of the ; after function declaration. Without it, the following code will output a Notice (Parse error syntax)
<?php $a = function($var) { $b = "Hello $var"; return $b; }; echo $a('World'); // output Hello World
  Anonymous functions are useful when using functions that require a callback function like array_filter or array_map do:
<?php $data = array(0, 3, 10); $data = array_filter($data, function($x) { return $x%2 == 0; }); // even var_dump($data); /* array (size=2) 0 => int 0 2 => int 10 */ $data = array(0, 3, 10); $data = array_map(function ($x) { return $x*$x; }, $data); // square var_dump($data); /* array (size=3) 0 => int 0 1 => int 9 2 => int 100 */