Questions about function return values

Source: Internet
Author: User
The following example shows how to call jj () in this case, why does one still print out null? it seems that the return value of the function is not used here. if I call this function, I just need to execute it to print $? Why are you still following... the following example shows how to call jj () in this case, why does one still print out null? it seems that the return value of the function is not used here. if I call this function, I just need to execute it to print $? Why is it related to the return value?

    function jj(){        $a=1;        echo $a;    }    $b=jj();    var_dump($b);

Reply content:

The following example shows how to call jj () in this case, why does one still print out null? it seems that the return value of the function is not used here. if I call this function, I just need to execute it to print $? Why is it related to the return value?

    function jj(){        $a=1;        echo $a;    }    $b=jj();    var_dump($b);

Print is only displayed on the screen. if you use this value, you must use return to receive it elsewhere. So here your function does not return anything, just prints 1, so variable B does not get any value, so it is null.

Because your function does not have a return statement, null is returned after execution.
Your function execution process:

  1. $ B = jj (); call jj () to assign a value to $ a, print $ a, No Return (return is null)

  2. Var_dump $ B, $ B is assigned the return value of jj (), so it is null

  3. After the execution is complete, the printed values 1 ($ a value) and null ($ B value) are left)

function jj(){        $a=1;        echo $a;        return $a;    }    $b=jj();    var_dump($b);

This is the expected output.

Your jj function directly outputs $ a without return, so $ B is empty.

$ B = jj (); // output 1 directly calls the jjvar_dump ($ B) function; // output the null print function? The result after the return value is not used: 1D: \ WWW \ demo. php: 15: int 1

Echo is the direct output, and return returns the location where it is called.

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.