Practical-6 CAP512: Variable functions and anonymous functions in PHP
Variable functions allow you 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.
<?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 ?>
Using Parameters with Variable Functions <?php function add($a, $b) { return $a + $b; } $addition = "add"; // Store function name in a variable $addition (5, 10); // Call function dynamically ?>
An anonymous function is a function without a name. These functions can be assigned to variables and passed as arguments to other functions.
Anonymous Function <?php $noname= function() { return "Hello, Anonymous Function!"; }; echo $noname(); ?>
Anonymous Function with Parameters $sum = function($a, $b) { return $a + $b; }; echo $sum(5, 7); // Output: 12 Using anonymous function convert the string in to upper case
<?php // Defining an anonymous function $square = function($x) { return $x * $x; }; // Calling the anonymous function echo $square(4); // Output: 16 ?>
Feature Variable Functions Anonymous 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
PHP Call By Reference function increment(&$num) { $num++; } $value = 5; increment($value); echo $value; // Output: 6 (original value is modified)

Data types and variables in php for writing

  • 1.
    Practical-6 CAP512: Variable functionsand anonymous functions in PHP
  • 2.
    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.
  • 6.
    Anonymous Function <?php $noname= function() { return"Hello, Anonymous Function!"; }; echo $noname(); ?>
  • 7.
    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)