Php variable instance details, variable instance details

Source: Internet
Author: User

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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.