Questions about the return value of a function

Source: Internet
Author: User
For the function return value or not very understand the following example, if you call JJ () directly print out a 1 why also print out a null it doesn't seem to use the function return value here. Does calling this function do the job of printing a $ A? Why is it related to the return value?

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

Reply content:

For the function return value or not very understand the following example, if you call JJ () directly print out a 1 why also print out a null it doesn't seem to use the function return value here. Does calling this function do the job of printing a $ A? Why is it related to the return value?

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

Printing is only displayed on the screen, if you use this value, you must return with return in order to receive it elsewhere. So here your function doesn't return anything, it just prints 1, so variable b doesn't get any value, so it's empty.

Because your function does not have a return statement, NULL is returned when the execution is complete.
Your function Execution process:

    1. $b = JJ (); Call JJ () to assign a value to $ A, print $ A, no return (null returned)

    2. Var_dump $b, $b The return value of the assigned JJ (), so it is null

    3. The entire execution is finished leaving a printed 1 ($a value), null ($b value)

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

So the estimate is the output you expect.

You have this JJ function that directly outputs $ A and does not return so $b is empty

$b=jj();//输出1 直接调用函数jjvar_dump($b);//输出null  打印函数?并没用return值加了return之后的结果如下:1D:\WWW\demo\demo\demo.php:15:int 1

Echo is the direct output, and return returns to the location where it was 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.