PHP: create_function - Manual in Deutsh
PHP: create_function - Manual in French
PHP: create_function - Manual in Polish

You Are At PHP: create_function - Manual Area


recent searches:
include functions , variable functions , post functions...




If you are new to PHP or just need to refresh your skills, this is the place to start. This series of tutorials will give you the basic knowledge you will need to create a simple PHP website.

PHP is a reflective programming language originally designed for producing dynamic web pages.[1] PHP is used mainly in server-side scripting, but can be used from a command line interface or in standalone graphical applications. Textual User Interfaces can also be created using ncurses.

PHP Manual
PrevNext

create_function

(PHP 4 >= 4.0.1, PHP 5)

create_function -- Create an anonymous (lambda-style) function

Description

string create_function ( string args, string code )

Creates an anonymous function from the parameters passed, and returns a unique name for it.

Parameters

Usually these parameters will be passed as single quote delimited strings. The reason for using single quoted strings, is to protect the variable names from parsing, otherwise, if you use double quotes there will be a need to escape the variable names, e.g. \$avar.

args

The function arguments.

code

The function code.

Return Values

Returns a unique function name as a string, or FALSE on error.

Examples

Example 1. Creating an anonymous function with create_function()

You can use this function, to (for example) create a function from information gathered at run time:

<?php
$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
?>

Or, perhaps to have general handler function that can apply a set of operations to a list of parameters:

Example 2. Making a general processing function with create_function()

<?php
function process($var1, $var2, $farr)
{
    foreach (
$farr as $f) {
        echo
$f($var1, $var2) . "\n";
    }
}

// create a bunch of math functions
$f1 = 'if ($a >=0) {return "b*a^2 = ".$b*sqrt($a);} else {return false;}';
$f2 = "return \"min(b^2+a, a^2,b) = \".min(\$a*\$a+\$b,\$b*\$b+\$a);";
$f3 = 'if ($a > 0 && $b != 0) {return "ln(a)/b = ".log($a)/$b; } else { return false; }';
$farr = array(
    
create_function('$x,$y', 'return "some trig: ".(sin($x) + $x*cos($y));'),
    
create_function('$x,$y', 'return "a hypotenuse: ".sqrt($x*$x + $y*$y);'),
    
create_function('$a,$b', $f1),
    
create_function('$a,$b', $f2),
    
create_function('$a,$b', $f3)
    );

echo
"\nUsing the first array of anonymous functions\n";
echo
"parameters: 2.3445, M_PI\n";
process(2.3445, M_PI, $farr);

// now make a bunch of string processing functions
$garr = array(
    
create_function('$b,$a', 'if (strncmp($a, $b, 3) == 0) return "** \"$a\" '.
    
'and \"$b\"\n** Look the same to me! (looking at the first 3 chars)";'),
    
create_function('$a,$b', '; return "CRCs: " . crc32($a) . " , ".crc32(b);'),
    
create_function('$a,$b', '; return "similar(a,b) = " . similar_text($a, $b, &$p) . "($p%)";')
    );
echo
"\nUsing the second array of anonymous functions\n";
process("Twas brilling and the slithy toves", "Twas the night", $garr);
?>

The above example will output:

Using the first array of anonymous functions
parameters: 2.3445, M_PI
some trig: -1.6291725057799
a hypotenuse: 3.9199852871011
b*a^2 = 4.8103313314525
min(b^2+a, a^2,b) = 8.6382729035898
ln(a/b) = 0.27122299212594

Using the second array of anonymous functions
** "Twas the night" and "Twas brilling and the slithy toves"
** Look the same to me! (looking at the first 3 chars)
CRCs: -725381282 , 1908338681
similar(a,b) = 11(45.833333333333%)

But perhaps the most common use for of lambda-style (anonymous) functions is to create callback functions, for example when using array_walk() or usort()

Example 3. Using anonymous functions as callback functions

<?php
$av
= array("the ", "a ", "that ", "this ");
array_walk($av, create_function('&$v,$k', '$v = $v . "mango";'));
print_r($av);
?>

The above example will output:

Array
(
  [0] => the mango
  [1] => a mango
  [2] => that mango
  [3] => this mango
)

an array of strings ordered from shorter to longer

<?php

$sv
= array("small", "larger", "a big string", "it is a string thing");
print_r($sv);

?>

The above example will output:

Array
(
  [0] => small
  [1] => larger
  [2] => a big string
  [3] => it is a string thing
)

sort it from longer to shorter

<?php

usort
($sv, create_function('$a,$b','return strlen($b) - strlen($a);'));
print_r($sv);

?>

The above example will output:

Array
(
  [0] => it is a string thing
  [1] => a big string
  [2] => larger
  [3] => small
)

Grzejniki
Wycieraczki
kredyt hipoteczny doradca finansowy wrocław
kredyt banki
Koszulki

Another Useful functions:


zend.creating | zend-macro.vcwd-creat | zend-api.zend-object-create-proxy | zend-api.virtual-creat | function.xslt-create | function.xmlrpc-server-destroy | function.xmlrpc-server-create | function.xmlrpc-server-create | function.xmlrpc-server-call-method | function.xml-parser-create | function.xml-parser-create-ns | function.win32-create-service | function.stream-context-create | function.sqlite-create-function | function.sqlite-create-aggregate | function.socket-create | function.socket-create-pair | function.socket-create-listen | function.sdo-dataobject-createdataobject | function.sdo-datafactory-create | function.sdo-das-xml-createdocument | function.sdo-das-xml-createdataobject | function.sdo-das-xml-create | function.sdo-das-relational-createrootdataobject | function.sca-soapproxy-createdataobject | function.sca-localproxy-createdataobject | function.sca-createdataobject | function.radius-create-request | function.px-create-fp | function.pspell-config-create | function.printer-create-pen | function.printer-create-font | function.printer-create-dc | function.printer-create-brush | function.pg-lo-create | function.pdo-sqlitecreatefunction | function.pdo-sqlitecreateaggregate | function.pdo-pgsqllobcreate | function.pdf-set-info-creator | function.pdf-create-textflow | function.pdf-create-pvf | function.pdf-create-gstate | function.pdf-create-fieldgroup | function.pdf-create-field | function.pdf-create-bookmark | function.pdf-create-annotation | function.pdf-create-action | function.openal-source-create | function.openal-context-create | function.openal-buffer-create | function.notes-nav-create | function.notes-create-note | function.notes-create-db | function.newt-create-grid | function.mysql-create-db | function.msql-createdb | function.msql-create-db | function.msession-create | function.mcrypt-create-iv | function.mcal-create-calendar | function.mailparse-msg-create | function.kadm5-create-principal | function.imap-createmailbox | function.imagecreatetruecolor | function.imagecreatefromxpm | function.imagecreatefromxbm | function.imagecreatefromwbmp | function.imagecreatefromstring | function.imagecreatefrompng | function.imagecreatefromjpeg | function.imagecreatefromgif | function.imagecreatefromgd2part | function.imagecreatefromgd2 | function.imagecreatefromgd | function.imagecreate | function.ifxus-create-slob | function.ifx-create-char | function.ifx-create-blob | function.icap-create-calendar | function.ibase-blob-create | function.harupage-createurlannotation | function.harupage-createtextannotation | function.harupage-createlinkannotation | function.harupage-createdestination | function.harudoc-createoutline | function.fdf-create | function.fbsql-create-db | function.fbsql-create-clob | function.fbsql-create-blob | function.domdocument-create-text-node | function.domdocument-create-processing-instruction | function.domdocument-create-entity-reference | function.domdocument-create-element | function.domdocument-create-element-ns | function.domdocument-create-comment | function.domdocument-create-cdata-section | function.domdocument-create-attribute | function.dom-domimplementation-createdocumenttype | function.dom-domimplementation-createdocument | function.dom-domdocument-createtextnode | function.dom-domdocument-createprocessinginstruction | function.dom-domdocument-createentityreference | function.dom-domdocument-createelementns | function.dom-domdocument-createelement | function.dom-domdocument-createdocumentfragment | function.dom-domdocument-createcomment | function.dom-domdocument-createcdatasection | function.dom-domdocument-createattributens | function.dom-domdocument-createattribute | function.dbplus-rcreate | function.dbase-create | function.date-create | function.create-function | function.cpdf-set-creator | function.com-create-guid |


PHP is a widely-used general-purpose scripting language that is especially suited for Web development and can be embedded into HTML. If you are new to PHP and want to get some idea of how it works, try the introductory tutorial. After that, check out the online manual, and the example archive sites and some of the other resources available in the links section.