PHP/Functions/Dynamic Function

Материал из Web эксперт
Перейти к: навигация, поиск

Calling a Function Dynamically

 
<html>
<head>
<title>Calling a Function Dynamically</title>
</head>
<body>
<div>
<?php
    function sayHello() {
      print "hello<br />";
    }
    $function_holder = "sayHello";
    $function_holder();
?>
</div>
</body>
</html>



Calling Functions Dynamically

<!--
   In PHP, you can call functions dynamically
   1.   Declare a function
   2.   Then declare a variable and assign the function name to the variable (as a string)
   3.   Then use the variable as a function
-->
<?php
  function addition ($a, $b){
      echo ($a + $b), "\n";
  }
  $result = "addition";
  $result (3,6);
?>



Calling Variable Functions

 
<?php 
function addvalues ($firstvalue = 0, $secondvalue = 0){ 
    return $firstvalue + $secondvalue; 
} 
function subtractvalues ($firstvalue = 0, $secondvalue = 0){ 
    return $firstvalue - $secondvalue; 
} 
function multiplyvalues ($firstvalue = 0, $secondvalue = 0){ 
    return $firstvalue * $secondvalue; 
} 
$firstvalue = 10; 
$secondvalue = 3; 
$whattodo = "addvalues"; 
echo $whattodo($firstvalue, $secondvalue) . "<br />"; 
$whattodo = "subtractvalues"; 
echo $whattodo($firstvalue, $secondvalue) . "<br />"; 
?>



Creating Dynamic Functions

 
<?php
  if ($_GET["go"] == "yes"){
    if ($_GET["loggedin"] == "true"){
      function dosomething (){
        $_GET["loggedin"] = false;
        echo "You have been successfully logged out.<br />";
      }
      
    }
    if ($_GET["loggedin"] == "false"){
      function dosomething (){
        $_GET["loggedin"] = true;
        echo "You have been successfully logged in.<br />";
      }
    }
    
    dosomething();
    
  }
  
  if ($_GET["loggedin"]){
    ?><a href="index.php?go=yes&amp;loggedin=true">click here to log out</a><?php
  } elseif (!$_GET["loggedin"]){
    ?><a href="index.php?go=yes&amp;loggedin=false">click here to log in</a><?php
  }
?>



return the function name from a function call or calculation

 
<?php 
function ShowSimple($val) { 
    echo "$val\n"; 
} 
function ShowComplex($val) { 
    echo "The value is " . number_format($val) . "\n"; 
} 
$v = 1234567; 
$a = "ShowSimple"; 
$b = "ShowComplex"; 
$a($v); 
$b($v); 
?>



String Manipulation

 
<html>
 
 <head> 
  <title>String Manipulation</title> 
 </head>
 <body>
  <form action="strings.php" method="post">
  <b>Enter some text here:</b><br>
  <textarea name="txt" rows="3" cols="45"></textarea> <br>
  <input type="radio" name="fcn" value="strlen">Find the text length
  <input type="radio" name="fcn" value="strrev">Reverse the text<br>
  <input type="radio" name="fcn" value="strtoupper">Change to all uppercase
  <input type="radio" name="fcn" value="strtolower">Change to all lowercase<br>
  <input type="radio" name="fcn" value="ucwords">Make the first letter of all words uppercase
  <hr>
  <input type="submit" value="Manipulate"> 
  </form>
 </body>
</html>
File: strings.php


<html>
 <head>
  <title>String Result</title>
 </head>
 <body>
 <?php 
 $txt = $_POST["txt"];
 $fcn = $_POST["fcn"];
 echo( $fcn( $txt ) ); ?>
 </body>
</html>



Using Arrays of Lambda Functions

 
<?php
$divisible = array(
    1 => create_function("$n",
        "echo "<p>$n is divisible by 1</p>";"),
    2 => create_function("$n",
        "if ($n % 2 == 0) {
            echo "<p>$n is divisible by 2.</p>"; }"),
    3 => create_function("$n",
        "if ($n % 3 == 0) {
            echo "<p>$n is divisible by 3.</p>"; }"),
    4 => create_function("$n",
        "if ($n % 4 == 0) {
            echo "<p>$n is divisible by 4.</p>"; }"),
    5 => create_function("$n",
        "if ($n % 5 == 0) { echo "<p>$n is divisible by 5.</p>"; }"),
    );
foreach($divisible as $func) {
    $func(2000);
}
?>



Using a variable function determined by some input variable

 
<?
function italian() {
     print "italian.";
}
function english() {
     print "english.";
}
$language = "italian";
$language();
?>



Variable function example

 
<?php
function foo() {
    echo "In foo()<br>\n";
}
function bar( $arg = " ) {
    echo "In bar(); argument was "$arg".<br>\n";
}
$func = "foo";
$func();
$func = "bar";
$func( "test" );
?>



Variable Function Names

 
<?php
function is_divisible_by_1($num) {
    echo "<p>Of course $num is divisible by 1, everything is!</p>";
}
function is_divisible_by_2($num) {
    if ($num % 2 == 0) {
        echo "<p>$num is divisible by 2.  That means it is even.</p>";
    }
}
function is_divisible_by_3($num) {
    if ($num % 3 == 0) {
        echo "<p>$num is divisible by 3.</p>";
    }
}
function is_divisible_by_4($num) {
    if ($num % 4 == 0) {
        echo "<p>$num is divisible by 4.  It is double-even!</p>";
    }
}
function is_divisible_by_5($num) {
    if ($num % 5 == 0) {
        echo "<p>$num is divisible by 5.  It ends in a 0 or a 5.</p>";
    }
}
for ($i = 5; $i > 0; $i--) {
    $var = "is_divisible_by_" . $i;
    $var(2000);
}
?>