InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
PHP
Creating Dynamic Functions in PHP
PHP

Creating Dynamic Functions in PHP

InfinityCoder November 23, 2016

You want to create and define a function as your program is running.

Use the closure syntax to define a function and store it in a variable:

1
2
3
4
$increment = 7;
$add = function($i, $j) use ($increment) { return $i + $j + $increment; };
 
$sum = $add(1, 2);

$sum is now 10. If you are using a version of PHP earlier than 5.3.0, use create_function() instead:

1
2
3
4
$increment = 7;
 
$add = create_function('$i,$j', 'return $i+$j + ' . $increment. ';');
$sum = $add(1, 2);

The closure syntax is much more pleasant than using create_function(). With create_function(), the argument list and function body are written as literal strings.

This means PHP can’t parse their syntax until runtime and you have to pay attention to single quoting and double quoting and variable interpolation rules.
With the closure syntax, PHP can do the same compile-time checking of your anonymous function as it does on the rest of your code.

You use the same syntax you’d use elsewhere for writing a function, with one exception: a use() declaration after the argument list can enumerate variables from the scope in which the closure is defined that should be available inside the closure.

In the preceding example, the use($incre ment) means that, inside the closure, $increment has the value (7) that it does in the scope in which the closure is defined.
A frequent use for anonymous functions is to create custom sorting functions for usort() or array_walk():

1
2
3
4
5
$files = array('ziggy.txt', '10steps.doc', '11pants.org', "frank.mov");
// sort files in reverse natural order
usort($files, function($a, $b) { return strnatcmp($b, $a); });
// Now $files is
// array('ziggy.txt', 'frank.mov','11pants.org','10steps.doc')

 

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Returning Failure in PHP
You want to indicate failure from a function. Return false: …

Returning Failure in PHP

Localizing Numbers in PHP
You want to display numbers in a locale-specific format. Use …

Localizing Numbers in PHP

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

  • fer on Turning a Dictionary into XML in Python
  • mahesh on Turning a Dictionary into XML in Python

Categories

  • Bash
  • PHP
  • Python
  • Uncategorized

InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

About Us

Start learning your desired programming language with InfinityQuest.com.

On our website you can access any tutorial that you want with video and code examples.

We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

Popular Tags

binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

Archives

  • June 2017
  • April 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
Copyright © 2021 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
Programming Tutorials | Sitemap