Variable functions allowyou to call a function using a variable that contains the function's name. This is useful when dynamically deciding which function to call at runtime.
3.
<?php function hello() { echo "Hello,variable function !!! n"; } function func() { echo "Hello, Normal function !!!"; } $func = "hello"; // Assigning function name to a variable $func(); // Calling the function using the variable func(); //normal call to function ?>
4.
Using Parameters withVariable Functions <?php function add($a, $b) { return $a + $b; } $addition = "add"; // Store function name in a variable $addition (5, 10); // Call function dynamically ?>
5.
An anonymous functionis a function without a name. These functions can be assigned to variables and passed as arguments to other functions.
Anonymous Function withParameters $sum = function($a, $b) { return $a + $b; }; echo $sum(5, 7); // Output: 12 Using anonymous function convert the string in to upper case
8.
<?php // Defining ananonymous function $square = function($x) { return $x * $x; }; // Calling the anonymous function echo $square(4); // Output: 16 ?>
9.
Feature Variable FunctionsAnonymous Functions Named or Anonymous? Named functions Anonymous (no name) How to Call? Via variable holding function name Via assigned variable Can be Passed as Argument? Yes Yes Can Access Outer Variables? No Yes (using use) Used in Callbacks? Rarely Commonly used
10.
PHP Call ByReference function increment(&$num) { $num++; } $value = 5; increment($value); echo $value; // Output: 6 (original value is modified)