dir.by  
Поиск >>

Компьютер, программы
→
PHP (Programming Language for Fast Writing of a Web Site)
 Pass the unnamed function as a parameter to the | PHP 
посмотрели 2643 раз
обновлено: 5 December 2024
  PHP  
// The ReadTextFile function contains 2 parameters:
// 1st peremeter is $arr (array)
// 2nd peremeter is $myFunction (function)
function GoInArray($arr, $myFunction)
{
     for($i=0; $i < count($arr); $i++)
          $myFunction($arr[$i]); // For each element, I call $myFunction
}


$my_arr1 = array(1, 6, 7, 8);

// The second parameter is to pass the unnamed function
GoInArray($my_arr1, function($item) {echo $item;} );
Example 2 Using use (..), the unnamed function sees the variable above
  PHP  
// The ReadTextFile function contains 2 parameters:
// 1st peremeter is $arr (array)
// 2nd peremeter is $myFunction (function)
function GoInArray($arr, $myFunction)
{
     for($i=0; $i < count($arr); $i++)
          $myFunction($arr[$i]); // For each element, I call $myFunction
}


$my_arr1 = array(1, 6, 7, 8);
$my_text = "Hello!";

// The second parameter is to pass the unnamed function
GoInArray($my_arr1, function($item) use ($my_text) {echo "$my_text $item";} );
Using use, an unnamed function sees the variable $my_text
 
← Previous topic
Anonymous function (no name). Function pointer in PHP
 
Next topic →
Array in PHP (Create an array, add elements, the length of the array, go through all the elements in the array)
 
Your feedback ... Comments ...
   
Your Name
Your comment (www links can only be added by a logged-in user)

Новость "Черная пятница" Dreame в Беларуси – две недели скидок до 3400 рублей
 
PHP Study
• What is PHP ?
Function
• Function in PHP. Example: function CalculateSum($value1, $value2) { ... }
• Passing parameters by value and by reference to a function in PHP
• Pass a function as a parameter to a function | PHP
Unnamed function
• Anonymous function (no name). Function pointer in PHP
• Pass the unnamed function as a parameter to the | using use (...) the unnamed function sees the variable above | PHP
Arrays
• Array in PHP (Create an array, add elements, the length of the array, go through all the elements in the array)
• Look for an element in an array in PHP | array_search
Key-value arrays
• Array (key-value) in PHP (Create an array, add elements, the length of the array, go through all the elements in the array)
Regular expressions
• Regular expressions
PHP Server
• Create an html file and javascript sends a post request to the php server. PHP server makes the answer | Why on php server $_REQUEST or $_POST array is empty
Framworks (frameworks)
• Zend Framework (PHP framework)
• Yii (PHP framework)
• CakePHP (framework)

  Ваши вопросы присылайте по почте: info@dir.by