The function of the @ symbol in PHP and the & symbol before PHP function

Source: Internet
Author: User
The function of the @ symbol in PHP and the & symbol before PHP function

. Role? Used to hide the wrong
2. When do I use it? Some of the errors will abort the program, but in fact it may only be a bug, because a small error and abort the program, it is certainly not good, so in the place where they expected to be wrong to add an "@", can prevent the error caused by the program to stop. For example "$con = @mysql_connect ($MYhost, $DB _name, $DB _password);"
3. What issues should be noted?
@ Just deceiving, it just hides the error, but it doesn't fix the error.

?

?

Answer to the second question:

?

?

The function of the & symbol before PHP function:
Look at the following code, which has a & symbol in front of the function test.
function &test () {
Declare a static variable
static $b = 0;
$b = $b +1;
Echo $b;
return $b;
}
The invocation method and the output result are as follows:
$a =test ();//This statement outputs a value of $b of 1
$a = 5;
$a =test ();//This statement outputs a value of $b of 2
$a =&test ();//This statement outputs a value of $b of 3
$a = 5;
$a =test ();//This statement outputs a value of $b of 6
Description
In this way $a=test () is not actually returned by a function reference, which is not the same as a normal function call.
As for the reason: this is the PHP rule
PHP rules through $a=&test (); The way to get is to return the reference to the function.
As for what is a reference return (the PHP manual says that reference return is used when you want to use a function to find out which variable the reference should be bound to.) )
This sentence on the manual may not be very well understood, see the explanations below:
$a =test () call the function, just assign the value of the function to $ A, and no change to $ A will affect the $b in the function.
And by calling the function by $a=&test (), his function is to return the memory address of the $b variable in the $b and the memory address of the $ A variable,
Point to the same place.
That produces the equivalent effect ($a =&b;) So change the value of $ A and change the value of $b at the same time, so in execution:
$a =&test ();
$a = 5;
Later, the value of the $b becomes 5

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