Php variable instance details, variable instance details
Php variable examples
Preface:
Recently, a new term "variable variables of php" was browsed by chance on the technical blog site. I checked it online and found it quite convenient, the following code is copied from the Internet to my local environment! "
The php manual says:
Sometimes it is convenient to use a variable name. That is to say, the variable name of a variable can be dynamically set and used. A common variable is set through declaration, for example:
$a = 'hello';$$a = 'world';echo "$a ${$a}";echo "$a $hello";
They all output: hello world.
To use variables in an array, you must solve an ambiguous problem. When writing $ a [1], the parser needs to know that $ a [1] is used as a variable, you still want $ a as a variable and retrieve the value of [1] In the variable. The syntax for solving this problem is to use $ {$ a [1]} For the first case and $ {$ a} [1] For the second case.
Class attributes can also be accessed through variable attribute names. Variable attributes will be parsed within the scope of the call. For example, for the $ foo-> $ bar expression, $ bar is parsed in the local range and its value is used for the attribute name of $ foo. The same applies when $ bar is an array unit. You can also use curly brackets to clearly define attribute names.
We can see that this is still in the fog. Now, in the daily html <form...> POST, many variables need to be processed. Generally, we will handle this:
$id = $_POST['id'];$name = $_POST['name'];$sex = $_POST['sex'];echo "<p>Your order is as follows:</p>";echo "<br />\$id=".$id;echo "<br />\$name=".$name;echo "<br />\$sex=".$sex;
But now the problem arises. If there are a lot of variables in POST, we will not be exhausted! Now the use of variable variables is coming. Let's write:
$ Array = array (); foreach ($ _ POST as $ key = >$ value) {$ array [$ key] = $ value;} echo '<pre> '; // wrap print_r ($ array); // is it convenient !!
Another example:
There are class A, class B, and class C. Both of them have their own functions or pass values on the front end. This time, they are two classes and one function is the same.
$class=$_GET['class'];$func=$_GET['func'];$obj=new $class();$obj->$func();
This is the idea of creating a single portal mode!
If you have any questions, please leave a message or go to the community on this site for discussion. Thank you for reading this article. Thank you for your support!