PhpDig.net

What is PhpDig?
PhpDig is a PHP MySQL based
Web Spider & Search Engine.




create_function

Name

create_function — Creates a function and returns a unique name for it.

Synopsis

string create_function(arguments, code);
string arguments: String containing the arguments to be passed to the function
string code: String containing the code for the function

Returns

A unique name for the function; FALSE on error

Description

create_function() provides an alternate way to create a function. Functions created via create_function() are given arbitrary unique names. Functions created in this fashion are useful for a variety of tasks, including creating function definitions at runtime and creating callback functions for use in functions such as array_walk() , register_shutdown_function() , and usort() .

The first argument should be a comma-separated list of the arguments to the anonymous function. The second argument should contain the code that makes up the body of the function. When creating these arguments, make sure that you are actually passing the function the same sort of literal values that you would use in a standard function definition. This is the most common pitfall when using create_function() . Take a close look at the following example to see how defining a function using create_function() compares to the regular function definition syntax:

// Standard function definition
function strip_non_num ($value) {
    return ereg_replace ('[^0-9]', '', $value);
}

// Same function definition using create_function()
// Note how both arguments are contained in single quotes.
// This prevents the $value inside these arguments from being replaced
// with the value stored in the variable. Look a few lines down to see the same
// function using double-quoted arguments.
// Also note that the second argument is a complete line of code
// - right down to the ending semicolon.
// Also note that this is a long and silly comment.
$function = create_function ('$value', 'return ereg_replace ("[^0-9]", "", $value);');

// Same function call using double-quoted arguments
// Note how the $ and quotes inside the quoted strings are escaped with backslashes
$function = create_function ("\$value", "return ereg_replace (\"[^0-9]\", \"\", \$value);");


Functions created by create_function() are called in almost the same fashion as a normal function:

// Continuing the above example

$cc_no = '4111 1111 1111 1111';

// Call the function defined via the standard method
print strip_non_num ($cc_no) . "\n";

// Call the function defined with create_function()
print $function ($cc_no);


Version

PHP Version: 4.0.1+

See also

To call functions and methods at runtime:

call_user_func()

call_user_method()

Example

Example 379. Use an anonymous function in usort()

<pre>
<?php
$array = array (1,2,3,4,5,6,7,8,9,10);

// Show the current order of the array
print_r ($array);

// Sort the values in the array by comparing the integer remainder
// left over from dividing the values by 5
usort ($array, create_function ('$a,$b', 'return $a % 5 - $b % 5;'));

// Show the new order
print_r ($array);

// For reference, here is the standard way of doing it
function mod_cmp ($a, $b) {
    // Added brackets are for readability
    return (($a % 5) - ($b % 5));
}

usort ($array, 'mod_cmp');
?>
</pre>



PHP Functions Essential Reference. Copyright © 2002 by New Riders Publishing (Authors: Zak Greant, Graeme Merrall, Torben Wilson, Brett Michlitsch). This material may be distributed only subject to the terms and conditions set forth in the Open Publication License, v1.0 or later (the latest version is presently available at http://www.opencontent.org/openpub/). The authors of this book have elected not to choose any options under the OPL. This online book was obtained from http://www.fooassociates.com/phpfer/ and is designed to provide information about the PHP programming language, focusing on PHP version 4.0.4 for the most part. The information is provided on an as-is basis, and no warranty or fitness is implied. All persons and entities shall have neither liability nor responsibility to any person or entity with respect to any loss or damage arising from the information contained in this book.

Powered by: vBulletin Version 3.0.7
Copyright ©2000 - 2005, Jelsoft Enterprises Ltd.
Copyright © 2001 - 2005, ThinkDing LLC. All Rights Reserved.