note 106235 deleted from function.eval by peehaa

From: Date: Mon, 09 Dec 2013 11:58:00 +0000
Subject: note 106235 deleted from function.eval by peehaa
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-197861@lists.php.net to get a copy of this message
Note Submitter: kevin at NOSPAM dot schinkenbraten dot de ---- Hey guys. With the eval()-function it is possible to create functions with specific names. Imagine if you have an Array and want to create some function of these names. <?php // Setting up the names for the functions. $names = Array ( "foo", "bar", "test", "foobar"); $prefix = "func_"; // Running a loop for the names and setting up the functions. foreach ($names as $j) { $function = $prefix.$j; $code = "function ".$function." () { echo '".$j."<br />'; }"; eval($code); } // Now we can run the functions easily now. func_foo(); func_bar(); func_test(); func_foobar(); ?> Output will be: foo bar test foobar -------- The point is that you don't have to run the function from a variable [create_function()]. I hope this is helpful for someone. ;) Kevin

« previous php.notes (#197861) next »