What is a mutable variable?
PHP provides one of the other types of variables-mutable variables. mutable variables allow us to dynamically change the name of a variable.
As you can see, PHP has a lot of latitude in this respect-all languages are allowed to change the values of variables, but there are not too many languages that allow variable types to be changed, and fewer languages support changing the names of variables.
This feature works by using the value of one variable as the name of another variable. For example:
$varname = ' tireqty ';
So, we can use
$$varname = 5;
This code is equivalent to:
$tireqty = 5;
in practice, we can use a loop statement and a variable to automatically process each form variable without having to list and use each one separately. In conjunction with the "For loop", examples are as follows:
main contents of orderform.html
<pre name= "code" class= "HTML" ><form action= "test.php" method= "POST" ><table border= "0" ><tr bgcolor= "#cccccc" ><td width= ">item</td><td width=" > QUANTITY</TD></TR><TR><TD>TIRES</TD><TD align= "center" ><input type= "text" Name= "Tireqty" size= "3" maxlength= "3"/></td></tr><tr><td>oil</td><td align= " Center "><input type=" text "name=" Oilqty "size=" 3 "maxlength=" 3 "/></td></tr><tr><td >spark plugs</td><td align= "center" ><input type= "text" name= "Sparkqty" size= "3" maxlength= "3"/> </TD></TR><TR><TD colspan= "2" align= "center" ><input type= "Submit" value= "Submit Order"/ ></td></tr></table></form>
Form processing File test.php
The traditional writing of the classic
<?php$oilqty = $_post[' Oilqty '; $tireqty = $_post[' Tireqty ']; $sparkqty = $_post[' sparkqty '];echo ' <p>your Order is as follows:</p> "; echo" <br/>\ $oilqty = ". $oilqty; echo" <br/>\ $tireqty = ". $tireqty; echo" < BR/>\ $sparkqty = ". $sparkqty;? >
If there are many form variables in the orderform.html, you can imagine that the above assignment statements will be very long, and we can use variable variables to optimize to the following very concise code:
<?phpforeach ($_post as $key + $value) {$ $key = $value;} echo "<p>your order is as follows:</p>", echo "<br/>\ $oilqty =". $oilqty; echo "<br/>\ $tireqty =". $tireqty, echo "<br/>\ $sparkqty =". $sparkqty;? >
What is a php mutable function?
PHP supports the concept of mutable functions. This means that if a variable name has parentheses after it, PHP will look for a function with the same name as the value of the variable and try to execute it. Variable functions can be used to implement some purposes, including callback functions, function tables, and so on.
Mutable functions cannot be used for example Echo,print,unset (), Isset (), Empty (), Include,require, and similar language constructs. You need to use your own wrapper functions to make these structures function as mutable functions
PHP variable Function Application Example:
For example:
We pass a value of name myfunc1 on the front end via get (WWW.ADMIN122.COM/?NAME=MYFUNC1), which is the name of the function you want to call
$f =$_get[' name ']; $f (); function myfunc1 () {}
One more example, this time with class and function.
There are Class A and Class B, and class C, and they all have their own functions.
Or the front-end value, this time is two class, a function
The same
$class =$_get[' class ']; $func =$_get[' func '); $obj =new $class (); $obj-$func ();
This is the idea of single-entry mode. The idea of the PHP framework.