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:
$b = JJ (); Call JJ () to assign a value to $ A, print $ A, no return (null returned)
Var_dump $b, $b The return value of the assigned JJ (), so it is null
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