Php interview questions

Source: Internet
Author: User
{Code...} Should I output myfunc (10) 20?
function myfunc($a){    echo $a + 10;}$val = 10;echo "myfunc($val)=".myfunc($val); //20myfunc(10)

Shouldn't myfunc (10) = 20 be output?

Reply content:
function myfunc($a){    echo $a + 10;}$val = 10;echo "myfunc($val)=".myfunc($val); //20myfunc(10)

Shouldn't myfunc (10) = 20 be output?

Tested:
The LZ interview is mainly about the execution sequence. The specific answer process is as follows:
A. echo "myfunc ($ val) = ";
In this case, the output result is myfunc (10) =, indicating that only functions are not parsed in double quotation marks and only variables are parsed.
B. echo "myfunc ($ val) =". myfunc ($ val );
After splicing the following function, the result is: 20 myfunc (10) =, indicating that the subsequent function first executes and outputs 20. Then the echo statement is executed.

Conclusion: The first step is to execute the function output 20, and then execute the echo statement. The result is: 20 myfunc (10) =

Joke... Your myfunc output is echo, not return... Don't be whimsical, okay... Execute the function first, then execute external echo ......

Function myfunc directly outputs the result, not return

You should be return $ a + 10;

What is echo in the function?

Why = I feel like 20 mytest (10) =, and the previous 20 should be the output of the later section.

In echo, the final result is output only after the values following the echo are calculated.
The myfunc function is executed in the calculation process, and the myfunc contains an echo, so 20 is output first.
In addition, the return value must be included in the function.

function myfunc($a){    return $a + 10;}$val = 10;echo "myfunc($val)=".myfunc($val);

function myfunc($a){    echo $a + 10;    echo "\n";}$val = 10;echo "myfunc($val)=".myfunc($val); //20myfunc(10)

This makes it much clearer.

I can see that you can directly echo in the function. You will understand the code.

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.