The difference between return,require,include braces and no parentheses in PHP

Source: Internet
Author: User
the difference between return,require,include braces and no parentheses in PHPJune 30, 2010 Tour Comments Read comments

In the collation code of the time to see such a sentence, return returned value as far as possible without parentheses, this is why.

Check out the Official handbook and find a sentence in http://cn2.php.net/manual/en/function.return.php:

Note: Your should never use parentheses around your return variable when returning by reference, as this is not Work. You can only return variables by reference and not the result of a statement. If use return ($a); Then you are not returning a variable, but the result of the expression ($a) (which is, course, the value of $a).

The meaning of translation into Chinese is:

Note: never use parentheses when returning a value with a reference, which is not feasible. A variable can only be returned by reference, not by the result of the statement. Instead of using the result of an expression, you can only return the referenced variable. If you use return ($a); Instead of returning a variable, it is the value of an expression ($a) (and, of course, that value is the $a value).

You may not understand this sentence, look at the following test code:

1
2
3
4 5 6 7 8
9
ten
-one 12
<?php
$good = ' good Chuzhi ';
$_server[' http_host '] = ' hao ';
function &changevariable ()
{return
 $_server[' Http_host ']; 
}
$good = &changevariable ();
Var_dump ($good);
$_server[' http_host ' = ' good ';
Var_dump ($good, $_server[' http_host ']);

This piece of code output:

String (3) "Hao"
String (4) "Good"
String (4) "Good"

If you put parentheses in the return variable, the code is as follows:

1
2
3
4 5 6 7 8
9
ten
-one 12
<?php
$good = ' good Chuzhi ';
$_server[' http_host '] = ' hao ';
function &changevariable ()
{return
 ($_server[' http_host ')); 
}
$good = &changevariable ();
Var_dump ($good);
$_server[' http_host ' = ' good ';
Var_dump ($good, $_server[' http_host ']);

This piece of code output:

String (3) "Hao"
String (3) "Hao"
String (4) "Good"

In the first example "return $_server[' http_host '"; without quotes, returns a reference to $_server[' Http_host ', when $good and $_server[' Http_host '] point to the same variable. So the $_server[' Http_host ' was modified, and the value of the $good changed accordingly.

And the second example if the variable after return has parentheses, the value of the expression in parentheses is evaluated first, and then the calculated result is returned. So $good points to "($_server[' http_host ')" The result of the calculation, and the variable that the $_server[' http_host '] points to is not the same, naturally modifies the value of $_server[' Http_host '], $ The value of the good remains unchanged. So for insurance, return returns a value that can be without parentheses without parentheses.

From the above, you can infer the difference between require include parentheses and no parentheses:

Include "a.php"; Include a.php directly in the

Include ("a.php");//First compute the value of ' (' a.php '), the result must be a.php, and then in the include "a.php"

Require is exactly the same as include.

As a result, the file paths that follow the include and require are not affected by the execution result (there is no reference problem), but the execution steps are added to reduce efficiency.

combined with the above analysis, whether it is include,require or return, for such operators, the following can not be parentheses without parentheses. Include and require are not distinguished by the results of the execution without parentheses, but are less efficient. Return is not only inefficient but also causes logical errors when returning references

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.