<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/css" href="http://commons.oreilly.com/wiki/skins/common/feed.css?97"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>http://commons.oreilly.com/wiki/index.php?title=PHP_Cookbook/Functions&amp;action=history&amp;feed=atom</id>
		<title>PHP Cookbook/Functions - Revision history</title>
		<link rel="self" type="application/atom+xml" href="http://commons.oreilly.com/wiki/index.php?title=PHP_Cookbook/Functions&amp;action=history&amp;feed=atom"/>
		<link rel="alternate" type="text/html" href="http://commons.oreilly.com/wiki/index.php?title=PHP_Cookbook/Functions&amp;action=history"/>
		<updated>2013-05-19T23:12:09Z</updated>
		<subtitle>Revision history for this page on the wiki</subtitle>
		<generator>MediaWiki 1.11.0</generator>

	<entry>
		<id>http://commons.oreilly.com/wiki/index.php?title=PHP_Cookbook/Functions&amp;diff=26130&amp;oldid=prev</id>
		<title>Newacct: /* Creating Dynamic Functions */</title>
		<link rel="alternate" type="text/html" href="http://commons.oreilly.com/wiki/index.php?title=PHP_Cookbook/Functions&amp;diff=26130&amp;oldid=prev"/>
				<updated>2010-01-30T02:28:50Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;span class=&quot;autocomment&quot;&gt;Creating Dynamic Functions&lt;/span&gt;&lt;/p&gt;

			&lt;table style=&quot;background-color: white; color:black;&quot;&gt;
			&lt;col class='diff-marker' /&gt;
			&lt;col class='diff-content' /&gt;
			&lt;col class='diff-marker' /&gt;
			&lt;col class='diff-content' /&gt;
			&lt;tr&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black;&quot;&gt;←Older revision&lt;/td&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black;&quot;&gt;Revision as of 02:28, 30 January 2010&lt;/td&gt;
			&lt;/tr&gt;
		&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 636:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 636:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt; $add = create_function('$i,$j', 'return $i+$j;');&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt; $add = create_function('$i,$j', 'return $i+$j;');&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;nbsp;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt; &lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;nbsp;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt; $add(1, 1); // returns 2&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;nbsp;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;nbsp;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;Starting in PHP 5.3, it is possible to define '''anonymous functions''':&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;nbsp;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;nbsp;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt; $add = function ($i, $j) { return $i+$j; };&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt; &lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt; &lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt; $add(1, 1); // returns 2&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt; $add(1, 1); // returns 2&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 650:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 656:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;=== See Also ===&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;=== See Also ===&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;-&lt;/td&gt;&lt;td style=&quot;background: #ffa; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;[[PHP Cookbook/Arrays#Sorting an Array by a Computable Field|Recipe 4.18]] for information on &amp;lt;tt&amp;gt;usort( )&amp;lt;/tt&amp;gt;; documentation on &amp;lt;tt&amp;gt;create_function( )&amp;lt;/tt&amp;gt; at ''http://www.php.net/create-function'' &lt;del style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;and &lt;/del&gt;on &amp;lt;tt&amp;gt;usort( )&amp;lt;/tt&amp;gt; at ''http://www.php.net/usort''.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;[[PHP Cookbook/Arrays#Sorting an Array by a Computable Field|Recipe 4.18]] for information on &amp;lt;tt&amp;gt;usort( )&amp;lt;/tt&amp;gt;; documentation on &amp;lt;tt&amp;gt;create_function( )&amp;lt;/tt&amp;gt; at ''http://www.php.net/create-function''&lt;ins style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;, &lt;/ins&gt;on &amp;lt;tt&amp;gt;usort( )&amp;lt;/tt&amp;gt; at ''http://www.php.net/usort&lt;ins style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;'', and on ''anonymous functions'' at ''http://www.php.net/functions.anonymous&lt;/ins&gt;''.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;!-- diff cache key wikicontent:diff:version:1.11a:oldid:7318:newid:26130 --&gt;
&lt;/table&gt;</summary>
		<author><name>Newacct</name></author>	</entry>

	<entry>
		<id>http://commons.oreilly.com/wiki/index.php?title=PHP_Cookbook/Functions&amp;diff=7318&amp;oldid=prev</id>
		<title>Docbook2Wiki: Initial conversion from Docbook</title>
		<link rel="alternate" type="text/html" href="http://commons.oreilly.com/wiki/index.php?title=PHP_Cookbook/Functions&amp;diff=7318&amp;oldid=prev"/>
				<updated>2008-03-07T13:36:04Z</updated>
		
		<summary type="html">&lt;p&gt;Initial conversion from Docbook&lt;/p&gt;

			&lt;table style=&quot;background-color: white; color:black;&quot;&gt;
			&lt;col class='diff-marker' /&gt;
			&lt;col class='diff-content' /&gt;
			&lt;col class='diff-marker' /&gt;
			&lt;col class='diff-content' /&gt;
			&lt;tr&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black;&quot;&gt;←Older revision&lt;/td&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black;&quot;&gt;Revision as of 13:36, 7 March 2008&lt;/td&gt;
			&lt;/tr&gt;
		&lt;/table&gt;</summary>
		<author><name>Docbook2Wiki</name></author>	</entry>

	<entry>
		<id>http://commons.oreilly.com/wiki/index.php?title=PHP_Cookbook/Functions&amp;diff=3115&amp;oldid=prev</id>
		<title>Evanlenz: 1 revision(s)</title>
		<link rel="alternate" type="text/html" href="http://commons.oreilly.com/wiki/index.php?title=PHP_Cookbook/Functions&amp;diff=3115&amp;oldid=prev"/>
				<updated>2008-03-06T22:30:01Z</updated>
		
		<summary type="html">&lt;p&gt;1 revision(s)&lt;/p&gt;

			&lt;table style=&quot;background-color: white; color:black;&quot;&gt;
			&lt;col class='diff-marker' /&gt;
			&lt;col class='diff-content' /&gt;
			&lt;col class='diff-marker' /&gt;
			&lt;col class='diff-content' /&gt;
			&lt;tr&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black;&quot;&gt;←Older revision&lt;/td&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black;&quot;&gt;Revision as of 22:30, 6 March 2008&lt;/td&gt;
			&lt;/tr&gt;
		&lt;/table&gt;</summary>
		<author><name>Evanlenz</name></author>	</entry>

	<entry>
		<id>http://commons.oreilly.com/wiki/index.php?title=PHP_Cookbook/Functions&amp;diff=3114&amp;oldid=prev</id>
		<title>Docbook2Wiki: Initial conversion from Docbook</title>
		<link rel="alternate" type="text/html" href="http://commons.oreilly.com/wiki/index.php?title=PHP_Cookbook/Functions&amp;diff=3114&amp;oldid=prev"/>
				<updated>2008-03-06T22:28:45Z</updated>
		
		<summary type="html">&lt;p&gt;Initial conversion from Docbook&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{PHP Cookbook/TOC}}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
Functions help you create organized and reusable code. They allow you to abstract out details so your code becomes more flexible and more readable. Without functions, it is impossible to write easily maintainable programs because you're constantly updating identical blocks of code in multiple places and in multiple files.&lt;br /&gt;
&lt;br /&gt;
With a function you pass a number of arguments in and get a value back:&lt;br /&gt;
&lt;br /&gt;
 // add two numbers together&lt;br /&gt;
 function add($a, $b) {&lt;br /&gt;
     return $a + $b;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 $total = add(2, 2);    // 4&lt;br /&gt;
&lt;br /&gt;
To declare a function, use the &amp;lt;tt&amp;gt;function&amp;lt;/tt&amp;gt; keyword, followed by the name of the function and any parameters in parentheses. To invoke a function, simply use the function name, specifying argument values for any parameters to the function. If the function returns a value, you can assign the result of the function to a variable, as shown in the previous example.&lt;br /&gt;
&lt;br /&gt;
You don't need to predeclare a function before you call it. PHP parses the entire file before it begins executing, so you can intermix function declarations and invocations. You can't, however, redefine a function in PHP. If PHP encounters a function with an identical name to one it's already found, it throws a fatal error and dies.&lt;br /&gt;
&lt;br /&gt;
Sometimes, the standard procedure of passing in a fixed number of arguments and getting one value back doesn't quite fit a particular situation in your code. Maybe you don't know ahead of time exactly how many parameters your function needs to accept. Or, you do know your parameters, but they're almost always the same values, so it's tedious to continue to repass them. Or, you want to return more than one value from your function.&lt;br /&gt;
&lt;br /&gt;
This chapter helps you use PHP to solve these types of problems. We begin by detailing different ways to pass arguments to a function. [[PHP Cookbook/Functions#Accessing Function Parameters|Recipe 6.2]] through [[PHP Cookbook/Functions#Creating Functions That Take a Variable Number of Arguments|Recipe 6.6]] cover passing arguments by value, reference, and as named parameters; assigning default parameter values; and functions with a variable number of parameters.&lt;br /&gt;
&lt;br /&gt;
The next four recipes are all about returning values from a function. [[PHP Cookbook/Functions#Returning Values by Reference|Recipe 6.7]] describes returning by reference, [[PHP Cookbook/Functions#Returning More Than One Value|Recipe 6.8]] covers returning more than one variable, [[PHP Cookbook/Functions#Skipping Selected Return Values|Recipe 6.9]] describes how to skip selected return values, and [[PHP Cookbook/Functions#Returning Failure|Recipe 6.10]] talks about the best way to return and check for failure from a function. The final three recipes show how to call variable functions, deal with variable scoping problems, and dynamically create a function. There's one recipe on function variables located in [[PHP Cookbook/Functions#Accessing Function Parameters|Recipe 6.2]]; if you want a variable to maintain its value between function invocations, see [[PHP Cookbook/Variables#Using Static Variables|Recipe 5.6]].&lt;br /&gt;
&lt;br /&gt;
== Accessing Function Parameters ==&lt;br /&gt;
&lt;br /&gt;
=== Problem ===&lt;br /&gt;
&lt;br /&gt;
You want to access the values passed to a function.&lt;br /&gt;
&lt;br /&gt;
=== Solution ===&lt;br /&gt;
&lt;br /&gt;
Use the names from the function prototype:&lt;br /&gt;
&lt;br /&gt;
 function commercial_sponsorship($letter, $number) { &lt;br /&gt;
     print &amp;quot;This episode of Sesame Street is brought to you by &amp;quot;;&lt;br /&gt;
     print &amp;quot;the letter $letter and number $number.\n&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 commercial_sponsorship('G', 3);&lt;br /&gt;
 commercial_sponsorship($another_letter, $another_number);&lt;br /&gt;
&lt;br /&gt;
=== Discussion ===&lt;br /&gt;
&lt;br /&gt;
Inside the function, it doesn't matter whether the values are passed in as strings, numbers, arrays, or another kind of variable. You can treat them all the same and refer to them using the names from the prototype.&lt;br /&gt;
&lt;br /&gt;
Unlike in C, you don't need to (and, in fact, can't) describe the type of variable being passed in. PHP keeps track of this for you.&lt;br /&gt;
&lt;br /&gt;
Also, unless specified, all values being passed into and out of a function are passed by value, not by reference. This means PHP makes a copy of the value and provides you with that copy to access and manipulate. Therefore, any changes you make to your copy don't alter the original value. Here's an example:&lt;br /&gt;
&lt;br /&gt;
 function add_one($number) {&lt;br /&gt;
     $number++;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 $number = 1;&lt;br /&gt;
 add_one($number);&lt;br /&gt;
 print &amp;quot;$number\n&amp;quot;;&lt;br /&gt;
 '''1'''&lt;br /&gt;
             &lt;br /&gt;
&lt;br /&gt;
If the variable was passed by reference, the value of &amp;lt;tt&amp;gt;$number&amp;lt;/tt&amp;gt; would be 2.&lt;br /&gt;
&lt;br /&gt;
In many languages, passing variables by reference also has the additional benefit of being significantly faster than by value. While this is also true in PHP, the speed difference is marginal. For that reason, we suggest passing variables by reference only when actually necessary and never as a performance-enhancing trick.&lt;br /&gt;
&lt;br /&gt;
=== See Also ===&lt;br /&gt;
&lt;br /&gt;
[[PHP Cookbook/Functions#Passing Values by Reference|Recipe 6.4]] to pass values by reference and [[PHP Cookbook/Functions#Returning Values by Reference|Recipe 6.7]] to return values by reference.&lt;br /&gt;
&lt;br /&gt;
== Setting Default Values for Function Parameters ==&lt;br /&gt;
&lt;br /&gt;
=== Problem ===&lt;br /&gt;
&lt;br /&gt;
You want a parameter to have a default value if the function's caller doesn't pass it. For example, a function to draw a table might have a parameter for border width, which defaults to 1 if no width is given.&lt;br /&gt;
&lt;br /&gt;
=== Solution ===&lt;br /&gt;
&lt;br /&gt;
Assign the default value to the parameters inside the function prototype:&lt;br /&gt;
&lt;br /&gt;
 function wrap_html_tag($string, $tag = 'b') {&lt;br /&gt;
     return &amp;quot;&amp;lt;$tag&amp;gt;$string&amp;lt;/$tag&amp;gt;&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
=== Discussion ===&lt;br /&gt;
&lt;br /&gt;
The example in the Solution sets the default tag value to &amp;lt;tt&amp;gt;b&amp;lt;/tt&amp;gt;, for bold. For example:&lt;br /&gt;
&lt;br /&gt;
 $string = 'I am some HTML';&lt;br /&gt;
 wrap_html_tag($string);&lt;br /&gt;
&lt;br /&gt;
returns:&lt;br /&gt;
&lt;br /&gt;
 &amp;amp;lt;b&amp;gt;I am some HTML&amp;amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This example:&lt;br /&gt;
&lt;br /&gt;
 wrap_html_tag($string, 'i');&lt;br /&gt;
&lt;br /&gt;
returns:&lt;br /&gt;
&lt;br /&gt;
 &amp;amp;lt;i&amp;gt;I am some HTML&amp;amp;lt;/i&amp;gt;&lt;br /&gt;
&lt;br /&gt;
There are two important things to remember when assigning default values. First, all parameters with default values must appear after parameters without defaults. Otherwise, PHP can't tell which parameters are omitted and should take the default value, and which arguments are overriding the default. So, &amp;lt;tt&amp;gt;wrap_html_tag( )&amp;lt;/tt&amp;gt; can't be defined as:&lt;br /&gt;
&lt;br /&gt;
 function wrap_html_tag($tag = 'i', $string)&lt;br /&gt;
&lt;br /&gt;
If you do this and pass &amp;lt;tt&amp;gt;wrap_html_tag( )&amp;lt;/tt&amp;gt; only a single argument, PHP assigns the value to &amp;lt;tt&amp;gt;$tag&amp;lt;/tt&amp;gt; and issues a warning complaining of a missing second argument.&lt;br /&gt;
&lt;br /&gt;
Second, the assigned value must be a constant — a string or a number. It can't be a variable. Again, using &amp;lt;tt&amp;gt;wrap_html_tag( )&amp;lt;/tt&amp;gt; as our example, you can't do this:&lt;br /&gt;
&lt;br /&gt;
 $my_favorite_html_tag = 'i';&lt;br /&gt;
 &lt;br /&gt;
 function wrap_html_tag($string, $tag = $my_favorite_html_tag) {&lt;br /&gt;
     ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
If you want to assign a default of nothing, one solution is to assign the empty string to your parameter:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;function wrap_html_tag($string, $tag = '') {&lt;br /&gt;
    if (empty($tag)) return $string;&lt;br /&gt;
    return &amp;quot;&amp;lt;$tag&amp;gt;$string&amp;lt;/$tag&amp;gt;&amp;quot;;&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function returns the original string, if no value is passed in for the &amp;lt;tt&amp;gt;$tag&amp;lt;/tt&amp;gt;. Or, if a (nonempty) tag is passed in, it returns the string wrapped inside of tags.&lt;br /&gt;
&lt;br /&gt;
Depending on circumstances, another option for the &amp;lt;tt&amp;gt;$tag&amp;lt;/tt&amp;gt; default value is either &amp;lt;tt&amp;gt;0&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;NULL&amp;lt;/tt&amp;gt;. In &amp;lt;tt&amp;gt;wrap_html_tag( )&amp;lt;/tt&amp;gt;, you don't want to allow an empty valued-tag. However, in some cases, the empty string can be an acceptable option. For instance, &amp;lt;tt&amp;gt;join( )&amp;lt;/tt&amp;gt; is often called on the empty string, after calling &amp;lt;tt&amp;gt;file( )&amp;lt;/tt&amp;gt; , to place a file into a string. Also, as the following code shows, you can use a default message if no argument is provided but an empty message if the empty string is passed:&lt;br /&gt;
&lt;br /&gt;
 function pc_log_db_error($message = NULL) {&lt;br /&gt;
     if (is_null($message)) {&lt;br /&gt;
         $message = 'Couldn't connect to DB';&lt;br /&gt;
     }&lt;br /&gt;
 &lt;br /&gt;
     error_log(&amp;quot;[DB] [$message]&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
=== See Also ===&lt;br /&gt;
&lt;br /&gt;
[[PHP Cookbook/Functions#Creating Functions That Take a Variable Number of Arguments|Recipe 6.6]] on creating functions that take a variable number of arguments.&lt;br /&gt;
&lt;br /&gt;
== Passing Values by Reference ==&lt;br /&gt;
&lt;br /&gt;
=== Problem ===&lt;br /&gt;
&lt;br /&gt;
You want to pass a variable to a function and have it retain any changes made to its value inside the function.&lt;br /&gt;
&lt;br /&gt;
=== Solution ===&lt;br /&gt;
&lt;br /&gt;
To instruct a function to accept an argument passed by reference instead of value, prepend an &amp;lt;tt&amp;gt;&amp;amp;&amp;lt;/tt&amp;gt; to the parameter name in the function prototype:&lt;br /&gt;
&lt;br /&gt;
 function wrap_html_tag(&amp;amp;$string, $tag = 'b') {&lt;br /&gt;
     $string = &amp;quot;&amp;lt;$tag&amp;gt;$string&amp;lt;/$tag&amp;gt;&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Now there's no need to return the string because the original is modified in-place.&lt;br /&gt;
&lt;br /&gt;
=== Discussion ===&lt;br /&gt;
&lt;br /&gt;
Passing a variable to a function by reference allows you to avoid the work of returning the variable and assigning the return value to the original variable. It is also useful when you want a function to return a boolean success value of &amp;lt;tt&amp;gt;true&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;false&amp;lt;/tt&amp;gt;, but you still want to modify argument values with the function.&lt;br /&gt;
&lt;br /&gt;
You can't switch between passing a parameter by value or reference; it's either one or the other. In other words, there's no way to tell PHP to optionally treat the variable as a reference or as a value.&lt;br /&gt;
&lt;br /&gt;
Actually, that statement isn't 100% true. If the configuration directive &amp;lt;tt&amp;gt;allow_call_time_pass_reference&amp;lt;/tt&amp;gt; is enabled, PHP lets you optionally pass a value by reference by prepending an ampersand to the variable's name. However, this feature has been deprecated since PHP 4.0 Beta 4, and PHP issues explicit warnings that this feature may go away in the future when you employ call-time pass-by-reference. Caveat coder.&lt;br /&gt;
&lt;br /&gt;
Also, if a parameter is declared to accept a value by reference, you can't pass a constant string (or number, etc.), or PHP will die with a fatal error.&lt;br /&gt;
&lt;br /&gt;
=== See Also ===&lt;br /&gt;
&lt;br /&gt;
[[PHP Cookbook/Functions#Returning Values by Reference|Recipe 6.7]] on returning values by reference.&lt;br /&gt;
&lt;br /&gt;
== Using Named Parameters ==&lt;br /&gt;
&lt;br /&gt;
=== Problem ===&lt;br /&gt;
&lt;br /&gt;
You want to specify your arguments to a function by name, instead of simply their position in the function invocation.&lt;br /&gt;
&lt;br /&gt;
=== Solution ===&lt;br /&gt;
&lt;br /&gt;
Have the function use one parameter but make it an associative array:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;function image($img) {&lt;br /&gt;
    $tag  = '&amp;lt;img src=&amp;quot;' . $img['src'] . '&amp;quot; ';&lt;br /&gt;
    $tag .= 'alt=&amp;quot;' . ($img['alt'] ? $img['alt'] : '') .'&amp;quot;&amp;gt;';&lt;br /&gt;
    return $tag;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
$image = image(array('src' =&amp;gt; 'cow.png', 'alt' =&amp;gt; 'cows say moo'));&lt;br /&gt;
$image = image(array('src' =&amp;gt; 'pig.jpeg'));&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Discussion ===&lt;br /&gt;
&lt;br /&gt;
While using named parameters makes the code inside your functions more complex, it ensures the calling code is easier to read. Since a function lives in one place but is called in many, this makes for more understandable code.&lt;br /&gt;
&lt;br /&gt;
When you use this technique, PHP doesn't complain if you accidentally misspell a parameter's name, so you need to be careful because the parser won't catch these types of mistakes. Also, you can't take advantage of PHP's ability to assign a default value for a parameter. Luckily, you can work around this deficit with some simple code at the top of the function:&lt;br /&gt;
&lt;br /&gt;
 function image($img) {&lt;br /&gt;
     if (! isset($img['src']))    { $img['src']    = 'cow.png';      }&lt;br /&gt;
     if (! isset($img['alt']))    { $img['alt']    = 'milk factory'; }&lt;br /&gt;
     if (! isset($img['height'])) { $img['height'] = 100;            }&lt;br /&gt;
     if (! isset($img['width']))  { $img['width']  = 50;             }&lt;br /&gt;
     ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Using the &amp;lt;tt&amp;gt;isset( )&amp;lt;/tt&amp;gt; function, check to see if a value for each parameter is set; if not, assign a default value.&lt;br /&gt;
&lt;br /&gt;
Alternatively, you can write a short function to handle this:&lt;br /&gt;
&lt;br /&gt;
 function pc_assign_defaults($array, $defaults) {&lt;br /&gt;
     $a = array( );&lt;br /&gt;
     foreach ($defaults as $d =&amp;gt; $v) {&lt;br /&gt;
         $a[$d] = isset($array[$d]) ? $array[$d] : $v;&lt;br /&gt;
     }&lt;br /&gt;
 &lt;br /&gt;
     return $a;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
This function loops through a series of keys from an array of defaults and checks if a given array, &amp;lt;tt&amp;gt;$array&amp;lt;/tt&amp;gt;, has a value set. If it doesn't, the function assigns a default value from &amp;lt;tt&amp;gt;$defaults&amp;lt;/tt&amp;gt;. To use it in the previous snippet, replace the top lines with:&lt;br /&gt;
&lt;br /&gt;
 function image($img) {&lt;br /&gt;
     $defaults = array('src'    =&amp;gt; 'cow.png',&lt;br /&gt;
                       'alt'    =&amp;gt; 'milk factory',&lt;br /&gt;
                       'height' =&amp;gt; 100,&lt;br /&gt;
                       'width'  =&amp;gt; 50&lt;br /&gt;
                      );&lt;br /&gt;
     $img = pc_assign_defaults($img, $defaults);&lt;br /&gt;
     ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
This is nicer because it introduces more flexibility into the code. If you want to modify how defaults are assigned, you only need to change it inside &amp;lt;tt&amp;gt;pc_assign_defaults( )&amp;lt;/tt&amp;gt; and not in hundreds of lines of code inside various functions. Also, it's clearer to have an array of name/value pairs and one line that assigns the defaults instead of intermixing the two concepts in a series of almost identical repeated lines.&lt;br /&gt;
&lt;br /&gt;
=== See Also ===&lt;br /&gt;
&lt;br /&gt;
[[PHP Cookbook/Functions#Creating Functions That Take a Variable Number of Arguments|Recipe 6.6]] on creating functions that accept a variable number of arguments.&lt;br /&gt;
&lt;br /&gt;
== Creating Functions That Take a Variable Number of Arguments ==&lt;br /&gt;
&lt;br /&gt;
=== Problem ===&lt;br /&gt;
&lt;br /&gt;
You want to define a function that takes a variable number of arguments.&lt;br /&gt;
&lt;br /&gt;
=== Solution ===&lt;br /&gt;
&lt;br /&gt;
Pass an array and place the variable arguments inside the array:&lt;br /&gt;
&lt;br /&gt;
 // find the &amp;quot;average&amp;quot; of a group of numbers&lt;br /&gt;
 function mean($numbers) {&lt;br /&gt;
     // initialize to avoid warnings&lt;br /&gt;
     $sum = 0;&lt;br /&gt;
 &lt;br /&gt;
     // the number of elements in the array&lt;br /&gt;
     $size = count($numbers);&lt;br /&gt;
 &lt;br /&gt;
     // iterate through the array and add up the numbers&lt;br /&gt;
     for ($i = 0; $i &amp;lt; $size; $i++) {&lt;br /&gt;
         $sum += $numbers[$i];&lt;br /&gt;
     }&lt;br /&gt;
 &lt;br /&gt;
     // divide by the amount of numbers&lt;br /&gt;
     $average = $sum / $size;&lt;br /&gt;
 &lt;br /&gt;
     // return average&lt;br /&gt;
     return $average;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 $mean = mean(array(96, 93, 97));&lt;br /&gt;
&lt;br /&gt;
=== Discussion ===&lt;br /&gt;
&lt;br /&gt;
There are two good solutions, depending on your coding style and preferences. The more traditional PHP method is the one described in the Solution. We prefer this method because using arrays in PHP is a frequent activity; therefore, all programmers are familiar with arrays and their behavior.&lt;br /&gt;
&lt;br /&gt;
So, while this method creates some additional overhead, bundling variables is commonplace. It's done in [[PHP Cookbook/Functions#Using Named Parameters|Recipe 6.5]] to create named parameters and in [[PHP Cookbook/Functions#Returning More Than One Value|Recipe 6.8]] to return more than one value from a function. Also, inside the function, the syntax to access and manipulate the array involves basic commands such as &amp;lt;tt&amp;gt;$array[$i]&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;count($array)&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
However, this can seem clunky, so PHP provides an alternative and allows you direct access to the argument list:&lt;br /&gt;
&lt;br /&gt;
 // find the &amp;quot;average&amp;quot; of a group of numbers&lt;br /&gt;
 function mean() {&lt;br /&gt;
     // initialize to avoid warnings&lt;br /&gt;
     $sum = 0;&lt;br /&gt;
 &lt;br /&gt;
     // the number of arguments passed to the function&lt;br /&gt;
     $size = func_num_args();&lt;br /&gt;
 &lt;br /&gt;
     // iterate through the arguments and add up the numbers&lt;br /&gt;
     for ($i = 0; $i &amp;lt; $size; $i++) {&lt;br /&gt;
         $sum += func_get_arg($i);&lt;br /&gt;
     }&lt;br /&gt;
 &lt;br /&gt;
     // divide by the amount of numbers&lt;br /&gt;
     $average = $sum / $size;&lt;br /&gt;
 &lt;br /&gt;
     // return average&lt;br /&gt;
     return $average;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 $mean = mean(96, 93, 97);&lt;br /&gt;
&lt;br /&gt;
This example uses a set of functions that return data based on the arguments passed to the function they are called from. First, &amp;lt;tt&amp;gt;func_num_args( )&amp;lt;/tt&amp;gt; returns an integer with the number of arguments passed into its invoking function — in this case, &amp;lt;tt&amp;gt;mean( )&amp;lt;/tt&amp;gt;. From there, you can then call &amp;lt;tt&amp;gt;func_get_arg( )&amp;lt;/tt&amp;gt; to find the specific argument value for each position.&lt;br /&gt;
&lt;br /&gt;
When you call &amp;lt;tt&amp;gt;mean(96, 93, 97)&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;func_num_args( )&amp;lt;/tt&amp;gt; returns &amp;lt;tt&amp;gt;3&amp;lt;/tt&amp;gt;. The first argument is in position &amp;lt;tt&amp;gt;0&amp;lt;/tt&amp;gt;, so you iterate from &amp;lt;tt&amp;gt;0&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;2&amp;lt;/tt&amp;gt;, not &amp;lt;tt&amp;gt;1&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;3&amp;lt;/tt&amp;gt;. That's what happens inside the &amp;lt;tt&amp;gt;for&amp;lt;/tt&amp;gt; loop where &amp;lt;tt&amp;gt;$i&amp;lt;/tt&amp;gt; goes from &amp;lt;tt&amp;gt;0&amp;lt;/tt&amp;gt; to less than &amp;lt;tt&amp;gt;$size&amp;lt;/tt&amp;gt;. As you can see, this is the same logic used in the first example in which an array was passed. If you're worried about the potential overhead from using &amp;lt;tt&amp;gt;func_get_arg( )&amp;lt;/tt&amp;gt; inside a loop, don't be. This version is actually faster than the array passing method.&lt;br /&gt;
&lt;br /&gt;
There is a third version of this function that uses &amp;lt;tt&amp;gt;func_num_args( )&amp;lt;/tt&amp;gt; to return an array containing all the values passed to the function. It ends up looking like hybrid between the previous two functions:&lt;br /&gt;
&lt;br /&gt;
 // find the &amp;quot;average&amp;quot; of a group of numbers&lt;br /&gt;
 function mean() {&lt;br /&gt;
     // initialize to avoid warnings&lt;br /&gt;
     $sum = 0;&lt;br /&gt;
 &lt;br /&gt;
     // load the arguments into $numbers&lt;br /&gt;
     $numbers = func_get_args();&lt;br /&gt;
 &lt;br /&gt;
     // the number of elements in the array&lt;br /&gt;
     $size = count($numbers);&lt;br /&gt;
 &lt;br /&gt;
     // iterate through the array and add up the numbers&lt;br /&gt;
     for ($i = 0; $i &amp;lt; $size; $i++) {&lt;br /&gt;
         $sum += $numbers[$i];&lt;br /&gt;
     }&lt;br /&gt;
 &lt;br /&gt;
     // divide by the amount of numbers&lt;br /&gt;
     $average = $sum / $size;&lt;br /&gt;
 &lt;br /&gt;
     // return average&lt;br /&gt;
     return $average;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 $mean = mean(96, 93, 97);&lt;br /&gt;
&lt;br /&gt;
Here you have the dual advantages of not needing to place the numbers inside a temporary array when passing them into &amp;lt;tt&amp;gt;mean( )&amp;lt;/tt&amp;gt;, but inside the function you can continue to treat them as if you did. Unfortunately, this method is slightly slower than the first two.&lt;br /&gt;
&lt;br /&gt;
=== See Also ===&lt;br /&gt;
&lt;br /&gt;
[[PHP Cookbook/Functions#Returning More Than One Value|Recipe 6.8]] on returning multiple values from a function; documentation on &amp;lt;tt&amp;gt;func_num_arg( )&amp;lt;/tt&amp;gt; at ''http://www.php.net/func-num-arg'', &amp;lt;tt&amp;gt;func_get_arg( )&amp;lt;/tt&amp;gt; at ''http://www.php.net/func-get-arg'', and &amp;lt;tt&amp;gt;func_get_args( )&amp;lt;/tt&amp;gt; at ''http://www.php.net/func-get-args''.&lt;br /&gt;
&lt;br /&gt;
== Returning Values by Reference ==&lt;br /&gt;
&lt;br /&gt;
=== Problem ===&lt;br /&gt;
&lt;br /&gt;
You want to return a value by reference, not by value. This allows you to avoid making a duplicate copy of a variable.&lt;br /&gt;
&lt;br /&gt;
=== Solution ===&lt;br /&gt;
&lt;br /&gt;
The syntax for returning a variable by reference is similar to passing it by reference. However, instead of placing an &amp;lt;tt&amp;gt;&amp;amp;&amp;lt;/tt&amp;gt; before the parameter, place it before the name of the function:&lt;br /&gt;
&lt;br /&gt;
 function &amp;amp;wrap_html_tag($string, $tag = 'b') {&lt;br /&gt;
     return &amp;quot;&amp;lt;$tag&amp;gt;$string&amp;lt;/$tag&amp;gt;&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Also, you must use the &amp;lt;tt&amp;gt;=&amp;amp;&amp;lt;/tt&amp;gt; assignment operator instead of plain &amp;lt;tt&amp;gt;=&amp;lt;/tt&amp;gt; when invoking the function:&lt;br /&gt;
&lt;br /&gt;
 $html =&amp;amp; wrap_html_tag($string);&lt;br /&gt;
&lt;br /&gt;
=== Discussion ===&lt;br /&gt;
&lt;br /&gt;
Unlike passing values into functions, in which an argument is either passed by value or by reference, you can optionally choose not to assign a reference and just take the returned value. Just use &amp;lt;tt&amp;gt;=&amp;lt;/tt&amp;gt; instead of &amp;lt;tt&amp;gt;=&amp;amp;&amp;lt;/tt&amp;gt;, and PHP assigns the value instead of the reference.&lt;br /&gt;
&lt;br /&gt;
=== See Also ===&lt;br /&gt;
&lt;br /&gt;
[[PHP Cookbook/Functions#Passing Values by Reference|Recipe 6.4]] on passing values by reference.&lt;br /&gt;
&lt;br /&gt;
== Returning More Than One Value ==&lt;br /&gt;
&lt;br /&gt;
=== Problem ===&lt;br /&gt;
&lt;br /&gt;
You want to return more than one value from a function.&lt;br /&gt;
&lt;br /&gt;
=== Solution ===&lt;br /&gt;
&lt;br /&gt;
Return an array and use &amp;lt;tt&amp;gt;list( )&amp;lt;/tt&amp;gt; to separate elements:&lt;br /&gt;
&lt;br /&gt;
 function averages($stats) {&lt;br /&gt;
     ...&lt;br /&gt;
     return array($median, $mean, $mode);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 list($median, $mean, $mode) = averages($stats);&lt;br /&gt;
&lt;br /&gt;
=== Discussion ===&lt;br /&gt;
&lt;br /&gt;
From a performance perspective, this isn't a great idea. There is a bit of overhead because PHP is forced to first create an array and then dispose of it. That's what is happening in this example:&lt;br /&gt;
&lt;br /&gt;
 function time_parts($time) {&lt;br /&gt;
     return explode(':', $time);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 list($hour, $minute, $second) = time_parts('12:34:56');&lt;br /&gt;
&lt;br /&gt;
You pass in a time string as you might see on a digital clock and call &amp;lt;tt&amp;gt;explode( )&amp;lt;/tt&amp;gt; to break it apart as array elements. When &amp;lt;tt&amp;gt;time_parts( )&amp;lt;/tt&amp;gt; returns, use &amp;lt;tt&amp;gt;list( )&amp;lt;/tt&amp;gt; to take each element and store it in a scalar variable. Although this is a little inefficient, the other possible solutions are worse because they can lead to confusing code.&lt;br /&gt;
&lt;br /&gt;
One alternative is to pass the values in by reference. However, this is somewhat clumsy and can be nonintuitive since it doesn't always make logical sense to pass the necessary variables into the function. For instance:&lt;br /&gt;
&lt;br /&gt;
 function time_parts($time, &amp;amp;$hour, &amp;amp;$minute, &amp;amp;$second) {&lt;br /&gt;
     list($hour, $minute, $second) = explode(':', $time);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 time_parts('12:34:56', $hour, $minute, $second);&lt;br /&gt;
&lt;br /&gt;
Without knowledge of the function prototype, there's no way to look at this and know &amp;lt;tt&amp;gt;$hour&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;$minute&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;$second&amp;lt;/tt&amp;gt; are, in essence, the return values of &amp;lt;tt&amp;gt;time_parts( )&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
You can also use global variables, but this clutters the global namespace and also makes it difficult to easily see which variables are being silently modified in the function. For example:&lt;br /&gt;
&lt;br /&gt;
 function time_parts($time) {&lt;br /&gt;
     global $hour, $minute, $second;&lt;br /&gt;
     list($hour, $minute, $second) = explode(':', $time);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 time_parts('12:34:56');&lt;br /&gt;
&lt;br /&gt;
Again, here it's clear because the function is directly above the call, but if the function is in a different file or written by another person, it'd be more mysterious and thus open to creating a subtle bug.&lt;br /&gt;
&lt;br /&gt;
Our advice is that if you modify a value inside a function, return that value and assign it to a variable unless you have a very good reason, such as significant performance issues. It's cleaner and easier to understand and maintain.&lt;br /&gt;
&lt;br /&gt;
=== See Also ===&lt;br /&gt;
&lt;br /&gt;
[[PHP Cookbook/Functions#Passing Values by Reference|Recipe 6.4]] on passing values by reference and [[PHP Cookbook/Functions#Accessing a Global Variable Inside a Function|Recipe 6.12]] for information on variable scoping.&lt;br /&gt;
&lt;br /&gt;
== Skipping Selected Return Values ==&lt;br /&gt;
&lt;br /&gt;
=== Problem ===&lt;br /&gt;
&lt;br /&gt;
A function returns multiple values, but you only care about some of them.&lt;br /&gt;
&lt;br /&gt;
=== Solution ===&lt;br /&gt;
&lt;br /&gt;
Omit variables inside of &amp;lt;tt&amp;gt;list( )&amp;lt;/tt&amp;gt; :&lt;br /&gt;
&lt;br /&gt;
 // Only care about minutes&lt;br /&gt;
 function time_parts($time) {&lt;br /&gt;
     return explode(':', $time);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 list(, $minute,) = time_parts('12:34:56');&lt;br /&gt;
&lt;br /&gt;
=== Discussion ===&lt;br /&gt;
&lt;br /&gt;
Even though it looks like there's a mistake in the code, the code in the Solution is valid PHP. This is most frequently seen when a programmer is iterating through an array using &amp;lt;tt&amp;gt;each( )&amp;lt;/tt&amp;gt;, but cares only about the array values:&lt;br /&gt;
&lt;br /&gt;
 while (list(,$value) = each($array)) {&lt;br /&gt;
     process($value);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
However, this is more clearly written using a &amp;lt;tt&amp;gt;foreach&amp;lt;/tt&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
 foreach ($array as $value) {&lt;br /&gt;
     process($value);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
To reduce confusion, we don't often use this feature, but if a function returns many values, and you only want one or two of them, this technique can come in handy. One example of this case is if you read in fields using &amp;lt;tt&amp;gt;fgetcsv( )&amp;lt;/tt&amp;gt; , which returns an array holding the fields from the line. In that case, you can use the following:&lt;br /&gt;
&lt;br /&gt;
 while ($fields = fgetcsv($fh, 4096)) {&lt;br /&gt;
     print $fields[2] . &amp;quot;\n&amp;quot;;  // the third field&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
If it's an internally written function and not built-in, you could also make the returning array have string keys, because it's hard to remember, for example, that array element 2 is associated with &amp;lt;tt&amp;gt;'rank'&amp;lt;/tt&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
 while ($fields = read_fields($filename)) {&lt;br /&gt;
     $rank = $fields['rank']; // the third field is now called rank&lt;br /&gt;
     print &amp;quot;$rank\n&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
However, here's the most efficient method:&lt;br /&gt;
&lt;br /&gt;
 while (list(,,$rank,,) = fgetcsv($fh, 4096)) {&lt;br /&gt;
     print &amp;quot;$rank\n&amp;quot;;         // directly assign $rank&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Be careful you don't miscount the amount of commas; you'll end up with a bug.&lt;br /&gt;
&lt;br /&gt;
=== See Also ===&lt;br /&gt;
&lt;br /&gt;
[[PHP Cookbook/Strings#Parsing Comma-Separated Data|Recipe 1.10]] for more on reading files using &amp;lt;tt&amp;gt;fgetcsv( )&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Returning Failure ==&lt;br /&gt;
&lt;br /&gt;
=== Problem ===&lt;br /&gt;
&lt;br /&gt;
You want to indicate failure from a function.&lt;br /&gt;
&lt;br /&gt;
=== Solution ===&lt;br /&gt;
&lt;br /&gt;
Return &amp;lt;tt&amp;gt;false&amp;lt;/tt&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
 function lookup($name) {&lt;br /&gt;
     if (empty($name)) { return false; }&lt;br /&gt;
     ...&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 if (false !== lookup($name)) { /* act upon lookup */ }&lt;br /&gt;
&lt;br /&gt;
=== Discussion ===&lt;br /&gt;
&lt;br /&gt;
I n PHP, non-true values aren't standardized and can easily cause errors. As a result, it's best if all your functions return the defined &amp;lt;tt&amp;gt;false&amp;lt;/tt&amp;gt; keyword because this works best when checking a logical value.&lt;br /&gt;
&lt;br /&gt;
Other possibilities are &amp;lt;tt&amp;gt;&amp;lt;nowiki&amp;gt;''&amp;lt;/nowiki&amp;gt;&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;0&amp;lt;/tt&amp;gt;. However, while all three evaluate to non-true inside an &amp;lt;tt&amp;gt;if&amp;lt;/tt&amp;gt;, there's actually a difference among them. Also, sometimes a return value of &amp;lt;tt&amp;gt;0&amp;lt;/tt&amp;gt; is a meaningful result, but you still want to be able to also return failure.&lt;br /&gt;
&lt;br /&gt;
For example, &amp;lt;tt&amp;gt;strpos( )&amp;lt;/tt&amp;gt; returns the location of the first substring within a string. If the substring isn't found, &amp;lt;tt&amp;gt;strpos( )&amp;lt;/tt&amp;gt; returns &amp;lt;tt&amp;gt;false&amp;lt;/tt&amp;gt;. If it is found, it returns an integer with the position. Therefore, to find a substring position, you might write:&lt;br /&gt;
&lt;br /&gt;
 if (strpos($string, $substring)) { /* found it! */ }&lt;br /&gt;
&lt;br /&gt;
However, if &amp;lt;tt&amp;gt;$substring&amp;lt;/tt&amp;gt; is found at the exact start of &amp;lt;tt&amp;gt;$string&amp;lt;/tt&amp;gt;, the value returned is &amp;lt;tt&amp;gt;0&amp;lt;/tt&amp;gt;. Unfortunately, inside the &amp;lt;tt&amp;gt;if&amp;lt;/tt&amp;gt;, this evaluates to &amp;lt;tt&amp;gt;false&amp;lt;/tt&amp;gt;, so the conditional is not executed. Here's the correct way to handle the return value of &amp;lt;tt&amp;gt;strpos( )&amp;lt;/tt&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
 if (false !== strpos($string, $substring)) { /* found it! */ }&lt;br /&gt;
&lt;br /&gt;
Also, &amp;lt;tt&amp;gt;false&amp;lt;/tt&amp;gt; is always guaranteed to be false — in the current version of PHP and forever more. Other values may not guarantee this. For example, in PHP 3, &amp;lt;tt&amp;gt;empty('0')&amp;lt;/tt&amp;gt; was &amp;lt;tt&amp;gt;true&amp;lt;/tt&amp;gt;, but it changed to &amp;lt;tt&amp;gt;false&amp;lt;/tt&amp;gt; in PHP 4.&lt;br /&gt;
&lt;br /&gt;
=== See Also ===&lt;br /&gt;
&lt;br /&gt;
The introduction to [[PHP Cookbook/Variables|Chapter 5]] for more on the truth values of variables; documentation on &amp;lt;tt&amp;gt;strpos( )&amp;lt;/tt&amp;gt; at ''http://www.php.net/strpos'' and &amp;lt;tt&amp;gt;empty( )&amp;lt;/tt&amp;gt; at ''http://www.php.net/empty''; information on migrating from PHP 3 to PHP 4 at ''http://www.php.net/migration4''.&lt;br /&gt;
&lt;br /&gt;
== Calling Variable Functions ==&lt;br /&gt;
&lt;br /&gt;
=== Problem ===&lt;br /&gt;
&lt;br /&gt;
You want to call different functions depending on a variable's value.&lt;br /&gt;
&lt;br /&gt;
=== Solution ===&lt;br /&gt;
&lt;br /&gt;
Use variable variables:&lt;br /&gt;
&lt;br /&gt;
 function eat_fruit($fruit) { print &amp;quot;chewing $fruit.&amp;quot;; }&lt;br /&gt;
 &lt;br /&gt;
 $function = 'eat_fruit';&lt;br /&gt;
 $fruit = 'kiwi';&lt;br /&gt;
 &lt;br /&gt;
 $function($fruit); // calls eat_fruit( )&lt;br /&gt;
&lt;br /&gt;
=== Discussion ===&lt;br /&gt;
&lt;br /&gt;
If you have multiple possibilities to call, use an associative array of function names:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;$dispatch = array(&lt;br /&gt;
    'add'      =&amp;gt; 'do_add',&lt;br /&gt;
    'commit'   =&amp;gt; 'do_commit',&lt;br /&gt;
    'checkout' =&amp;gt; 'do_checkout',&lt;br /&gt;
    'update'   =&amp;gt; 'do_update'&lt;br /&gt;
);&lt;br /&gt;
&lt;br /&gt;
$cmd = (isset($_REQUEST['command']) ? $_REQUEST['command'] : '');&lt;br /&gt;
&lt;br /&gt;
if (array_key_exists($cmd, $dispatch)) {&lt;br /&gt;
    $function = $dispatch[$cmd];&lt;br /&gt;
    $function(); // call function&lt;br /&gt;
} else {&lt;br /&gt;
    error_log(&amp;quot;Unknown command $cmd&amp;quot;);&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This code takes the command name from a request and executes that function. Note the check to see that the command is in a list of acceptable command. This prevents your code from calling whatever function was passed in from a request, such as &amp;lt;tt&amp;gt;phpinfo( )&amp;lt;/tt&amp;gt; . This makes your code more secure and allows you to easily log errors.&lt;br /&gt;
&lt;br /&gt;
Another advantage is that you can map multiple commands to the same function, so you can have a long and a short name:&lt;br /&gt;
&lt;br /&gt;
 $dispatch = array(&lt;br /&gt;
     'add'      =&amp;gt; 'do_add',&lt;br /&gt;
     'commit'   =&amp;gt; 'do_commit',   'ci' =&amp;gt; 'do_commit', &lt;br /&gt;
     'checkout' =&amp;gt; 'do_checkout', 'co' =&amp;gt; 'do_checkout',&lt;br /&gt;
     'update'   =&amp;gt; 'do_update',   'up' =&amp;gt; 'do_update'&lt;br /&gt;
 );&lt;br /&gt;
&lt;br /&gt;
=== See Also ===&lt;br /&gt;
&lt;br /&gt;
[[PHP Cookbook/Variables#Creating a Dynamic Variable Name|Recipe 5.5]] for more on variable variables.&lt;br /&gt;
&lt;br /&gt;
== Accessing a Global Variable Inside a Function ==&lt;br /&gt;
&lt;br /&gt;
=== Problem ===&lt;br /&gt;
&lt;br /&gt;
You need to access a global variable inside a function.&lt;br /&gt;
&lt;br /&gt;
=== Solution ===&lt;br /&gt;
&lt;br /&gt;
Bring the global variable into local scope with the &amp;lt;tt&amp;gt;global&amp;lt;/tt&amp;gt; keyword:&lt;br /&gt;
&lt;br /&gt;
 function eat_fruit($fruit) {&lt;br /&gt;
    global $chew_count;&lt;br /&gt;
  &lt;br /&gt;
    for ($i = $chew_count; $i &amp;gt; 0; $i--) {&lt;br /&gt;
        ...&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Or reference it directly in &amp;lt;tt&amp;gt;$GLOBALS&amp;lt;/tt&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
 function eat_fruit($fruit) {&lt;br /&gt;
    for ($i = $GLOBALS['chew_count']; $i &amp;gt; 0; $i--) {&lt;br /&gt;
        ...&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
=== Discussion ===&lt;br /&gt;
&lt;br /&gt;
If you use a number of global variables inside a function, the &amp;lt;tt&amp;gt;global&amp;lt;/tt&amp;gt; keyword may make the syntax of the function easier to understand, especially if the global variables are interpolated in strings.&lt;br /&gt;
&lt;br /&gt;
You can use the &amp;lt;tt&amp;gt;global&amp;lt;/tt&amp;gt; keyword to bring multiple global variables into local scope by specifying the variables as a comma-separated list:&lt;br /&gt;
&lt;br /&gt;
 global $age,$gender,shoe_size;&lt;br /&gt;
&lt;br /&gt;
You can also specify the names of global variables using variable variables:&lt;br /&gt;
&lt;br /&gt;
 $which_var = 'age';&lt;br /&gt;
 global $$which_var; // refers to the global variable $age&lt;br /&gt;
&lt;br /&gt;
However, if you call &amp;lt;tt&amp;gt;unset( )&amp;lt;/tt&amp;gt; on a variable brought into local scope using the &amp;lt;tt&amp;gt;global&amp;lt;/tt&amp;gt; keyword, the variable is unset only within the function. To unset the variable in the global scope, you must call &amp;lt;tt&amp;gt;unset( )&amp;lt;/tt&amp;gt; on the element of the &amp;lt;tt&amp;gt;$GLOBALS&amp;lt;/tt&amp;gt; array:&lt;br /&gt;
&lt;br /&gt;
 $food =  'pizza';&lt;br /&gt;
 $drink = 'beer';&lt;br /&gt;
 &lt;br /&gt;
 function party( ) {&lt;br /&gt;
     global $food, $drink;&lt;br /&gt;
     &lt;br /&gt;
     unset($food);             // eat pizza&lt;br /&gt;
     unset($GLOBALS['drink']); // drink beer&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 print &amp;quot;$food: $drink\n&amp;quot;;&lt;br /&gt;
 party( );&lt;br /&gt;
 print &amp;quot;$food: $drink\n&amp;quot;;&lt;br /&gt;
 '''pizza: beer'''&lt;br /&gt;
                '''pizza:'''&lt;br /&gt;
             &lt;br /&gt;
&lt;br /&gt;
You can see that &amp;lt;tt&amp;gt;$food&amp;lt;/tt&amp;gt; stayed the same, while &amp;lt;tt&amp;gt;$drink&amp;lt;/tt&amp;gt; was unset. Declaring a variable &amp;lt;tt&amp;gt;global&amp;lt;/tt&amp;gt; inside a function is similar to assigning a reference of the global variable to the local one:&lt;br /&gt;
&lt;br /&gt;
 $food = &amp;amp;GLOBALS['food'];&lt;br /&gt;
&lt;br /&gt;
=== See Also ===&lt;br /&gt;
&lt;br /&gt;
Documentation on variable scope at ''http://www.php.net/variables.scope'' and variable references at ''http://www.php.net/language.references''.&lt;br /&gt;
&lt;br /&gt;
== Creating Dynamic Functions ==&lt;br /&gt;
&lt;br /&gt;
=== Problem ===&lt;br /&gt;
&lt;br /&gt;
You want to create and define a function as your program is running.&lt;br /&gt;
&lt;br /&gt;
=== Solution ===&lt;br /&gt;
&lt;br /&gt;
Use &amp;lt;tt&amp;gt;create_function( )&amp;lt;/tt&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
 $add = create_function('$i,$j', 'return $i+$j;');&lt;br /&gt;
 &lt;br /&gt;
 $add(1, 1); // returns 2&lt;br /&gt;
&lt;br /&gt;
=== Discussion ===&lt;br /&gt;
&lt;br /&gt;
The first parameter to &amp;lt;tt&amp;gt;create_function( )&amp;lt;/tt&amp;gt; is a string that contains the arguments for the function, and the second is the function body. Using &amp;lt;tt&amp;gt;create_function( )&amp;lt;/tt&amp;gt; is exceptionally slow, so if you can predefine the function, it's best to do so.&lt;br /&gt;
&lt;br /&gt;
The most frequently used case of &amp;lt;tt&amp;gt;create_function( )&amp;lt;/tt&amp;gt; in action is to create custom sorting functions for &amp;lt;tt&amp;gt;usort( )&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;array_walk( )&amp;lt;/tt&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
 // sort files in reverse natural order&lt;br /&gt;
 usort($files, create_function('$a, $b', 'return strnatcmp($b, $a);'));&lt;br /&gt;
&lt;br /&gt;
=== See Also ===&lt;br /&gt;
&lt;br /&gt;
[[PHP Cookbook/Arrays#Sorting an Array by a Computable Field|Recipe 4.18]] for information on &amp;lt;tt&amp;gt;usort( )&amp;lt;/tt&amp;gt;; documentation on &amp;lt;tt&amp;gt;create_function( )&amp;lt;/tt&amp;gt; at ''http://www.php.net/create-function'' and on &amp;lt;tt&amp;gt;usort( )&amp;lt;/tt&amp;gt; at ''http://www.php.net/usort''.&lt;/div&gt;</summary>
		<author><name>Docbook2Wiki</name></author>	</entry>

	</feed>