The Magical magic of PHP quote

Source: Internet
Author: User
The magical use of PHP references

In: http://bardo.iteye.com/admin/blogs/848136? I have already described in detail what a reference is. Here is a talk about the magical use of references.

1, referring to the parameters, this has two functions, one, so that the external variables and functions in the variable synchronization changes. Second, the implementation has no return but can return multiple parameters.

2, for large data structure, reduce replication, thereby reducing memory consumption.

This is a common use. References in fact, there are some wonderful uses that are not known. We have just found one:

?

If there is code in a file, if we use it in the shell state:

$host =$_server[' Http_host '];

Notice information will appear in the log. Of course, you shut it down and it doesn't mean it doesn't produce. But in order for the web and shell to work, let's do this:

One

Use Error_reporting (245) to turn it off.

Second, directly prevent it from appearing:

$host =@$_server[' Http_host '];

Third, use the ternary operator.

$host = (isset ($_server[' http_host '))? $_server[' Http_host ']: ';

?

But this is a common approach.

In fact, we also have a way to let it not appear notice information:

?

$host =& $_server[' http_host '];

?

In this way, we guarantee that we will never use isset when we pass the argument, or @

?

We only use isset when the variables are actually used. This is not only a reduction in the number of code characters, but also a means of speeding up operational efficiency.

?

OK, now you might as well try it out, if the code below is running like this.

?

   Error_reporting (E_all);    $ar =array ();    $b =& $ar [' Test '];  You can try the previous method in this line    $c = ' all '. $b. ' ";    Echo ($c);

?

Of course, using references, the biggest consideration is that you have to make sure that you can't change it in use. Otherwise, causes the original variable to synchronize the change, you must anticipate beforehand.

?

So, if it's not predictable, use @

?

1 Floor Input 2011-04-22

Really magical!

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