Php-mysql Course

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.
<?php
$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).
<?php
$website = function($name) {
  return 'http://www.'. $name. '.net';
};

echo $website('coursesweb');           // https://coursesweb.net
echo $website('marplo');               // https://marplo.net
?>

3. Closures can also be used inside another function. In the following example it is used closures to create HTML List elements.
<?php
// 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.
<?php
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.
<?php
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

HTML
CSS
JavaScript
PHP-MySQL
Which attribute is used in <a> tag for the address of the link?
src href rel
<a href="http://coursesweb.net/" title="CoursesWeb.net">CoursesWeb.net</a>
Which CSS property sets the type of the text font?
font-family text-decoration font-size
h2 {
  font-family:"Calibri",sans-serif;
}
What instruction selects all the <div> tags with class="cls"?
querySelector("div.cls") getElementsByTagName("div") querySelectorAll("div.cls")
var elm_list = document.querySelectorAll("div.cls");
var nr_elms = elm_list.length;       // number of selected items
alert(nr_elms);
Indicate the function that can be used to get the sum of values in an array.
array_sum() array_diff() array_shift()
$arr =[1, 2, 3, 4);
$arr_sum = array_sum($arr);
echo $arr_sum;       // 10
PHP Anonymous functions - Closures

Last accessed pages

  1. elmPosiz - Get position, size and visibility in viewport of HTML element (1413)
  2. Laravel Middleware (403)
  3. PHP Unzipper - Extract Zip, Rar Archives (26781)
  4. innerHTML in PHP (15281)
  5. Multidimensional arrays and array functions (8226)

Popular pages this month

  1. PHP Unzipper - Extract Zip, Rar Archives (740)
  2. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (498)
  3. SHA1 Encrypt data in JavaScript (391)
  4. Create simple Website with PHP (366)
  5. Read Excel file data in PHP - PhpExcelReader (355)