PHP/Functions/Dynamic Function

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

Calling a Function Dynamically

   <source lang="html4strict">

<html> <head> <title>Calling a Function Dynamically</title> </head> <body>

<?php

   function sayHello() {
     print "hello
"; } $function_holder = "sayHello"; $function_holder();

?>

</body> </html>

 </source>
   
  


Calling Functions Dynamically

   <source lang="html4strict">

<?php

 function addition ($a, $b){
     echo ($a + $b), "\n";
 }
 $result = "addition";
 $result (3,6);

?>

      </source>
   
  


Calling Variable Functions

   <source lang="html4strict">

<?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) . "
"; $whattodo = "subtractvalues"; echo $whattodo($firstvalue, $secondvalue) . "
"; ?>

 </source>
   
  


Creating Dynamic Functions

   <source lang="html4strict">

<?php

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

?>

 </source>
   
  


return the function name from a function call or calculation

   <source lang="html4strict">

<?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); ?>

 </source>
   
  


String Manipulation

   <source lang="html4strict">

<html>

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

 <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>

 </source>
   
  


Using Arrays of Lambda Functions

   <source lang="html4strict">

<?php $divisible = array(

   1 => create_function("$n",
"echo "

$n is divisible by 1

";"),
   2 => create_function("$n",
       "if ($n % 2 == 0) {
echo "

$n is divisible by 2.

"; }"),
   3 => create_function("$n",
       "if ($n % 3 == 0) {
echo "

$n is divisible by 3.

"; }"),
   4 => create_function("$n",
       "if ($n % 4 == 0) {
echo "

$n is divisible by 4.

"; }"),
   5 => create_function("$n",
"if ($n % 5 == 0) { echo "

$n is divisible by 5.

"; }"),
   );

foreach($divisible as $func) {

   $func(2000);

} ?>

 </source>
   
  


Using a variable function determined by some input variable

   <source lang="html4strict">

<? function italian() {

    print "italian.";

} function english() {

    print "english.";

} $language = "italian"; $language(); ?>

 </source>
   
  


Variable function example

   <source lang="html4strict">

<?php function foo() {

   echo "In foo()
\n";

} function bar( $arg = " ) {

   echo "In bar(); argument was "$arg".
\n";

} $func = "foo"; $func(); $func = "bar"; $func( "test" ); ?>

 </source>
   
  


Variable Function Names

   <source lang="html4strict">

<?php function is_divisible_by_1($num) {

echo "

Of course $num is divisible by 1, everything is!

";

} function is_divisible_by_2($num) {

   if ($num % 2 == 0) {
echo "

$num is divisible by 2. That means it is even.

";
   }

} function is_divisible_by_3($num) {

   if ($num % 3 == 0) {
echo "

$num is divisible by 3.

";
   }

} function is_divisible_by_4($num) {

   if ($num % 4 == 0) {
echo "

$num is divisible by 4. It is double-even!

";
   }

} function is_divisible_by_5($num) {

   if ($num % 5 == 0) {
echo "

$num is divisible by 5. It ends in a 0 or a 5.

";
   }

} for ($i = 5; $i > 0; $i--) {

   $var = "is_divisible_by_" . $i;
   $var(2000);

} ?>

 </source>