Starting with the version 5.3, PHP introduced Anonymous functions, known as Closures.
This type of functions have no specified name. They are most useful as the value of callback.

1. Example, Anonymous function as callback function.
$arr = array(' hello word ', ' Have <b>good day</b>');

// removes tags and whitespace from the beginning and end of each array element
// capitalize the first character of each word
$arr = array_map(function($elm){
 return ucwords(trim(strip_tags($elm)));
}, $arr);

var_export($arr);            // array ( 0 => 'Hello Word', 1 => 'Have Good Day' )

2. Closures can also be used as the values of variables (notice how the function is called, using the variable name, including the $ character).
$website = function($name) {
  return 'http://www.'. $name. '.net';

echo $website('coursesweb');           //
echo $website('marplo');               //

3. Closures can also be used inside another function. In the following example it is used closures to create HTML List elements.
// receives an array with elements that will form the <li>
function ulList($ar_items) {
  $li = function($item) {
    return '<li>'. $item. '</li>';

  // traverse the array received in parameter, calls $li() to create the <ul><li>
  $ul = '<ul>';
  foreach($ar_items as $item) {
    $ul .= $li($item);
  $ul .= '<ul>';

  return $ul;

$ar_items = array('li 1', 'li 2', 'li 3');
echo ulList($ar_items);         // <ul><li>li 1</li><li>li 2</li><li>li 3</li><ul>

- Another example with anonymous function inside other function. Notice the using of the use() , to can use the outside variables inside anonymous function.
function test($a) {
  $b = $a / 2;
  $c = function($a) use($b) {
    return $a + $b;

  return $c($a);

echo test(10);         // 15

4. Closures can be a useful tool for object-oriented programming. Starting with PHP 5.4 , $this can be used in anonymous functions.
class A {
  private $value = 1;
  public function getVal() {
    return function() { return $this->value * 2; };

$a = new A;
echo $a->getVal();        // 2

Daily Test with Code Example

Which HTML5 tag is indicated to be used as container for menu with navigation links in Web site?
<section> <nav> <article>
 <li><a href="" title="CSS Course">CSS Course</a></li>
 <li><a href="" title="Flash Games">Flash Games</a></li>
Which CSS property shifts an item horizontally to the left or right of where it was?
text-align clear float
.some_class {
  width: 30%;
  float: left;
Click on the Math object method which returns x, rounded downwards to the nearest integer.
Math.ceil(x) Math.abs(x) Math.floor(x)
var num = 12.34567;
num = Math.floor(num);
alert(num);       // 12
Indicate the PHP function which returns the number of characters in string.
mb_strlen() count() stristr()
$str = "string with utf-8 chars åèö";
$nrchr = mb_strlen($str);
echo $nrchr;        // 30
PHP Anonymous functions - Closures

Last accessed pages

  1. JavaScript base64 encode decode (3644)
  2. PHP MySQL - WHERE and LIKE (17939)
  3. SHA512 Encrypt hash in JavaScript (9800)
  4. Insert, Select and Update NULL value in MySQL (26447)
  5. Register and show online users and visitors (22698)

Popular pages this month

  1. Read Excel file data in PHP - PhpExcelReader (303)
  2. PhpSpreadsheet - Read, Write Excel and LibreOffice Calc files (250)
  3. Register and show online users and visitors (240)
  4. Courses Web: PHP-MySQL JavaScript Ajax HTML CSS Flash-AS3 (237)
  5. Insert, Select and Update NULL value in MySQL (149)