I just learned PHP for a few days and I don’t quite understand.returnis not used to carry and return a value infunction. .
Today I saw someone else invideo. In an if statement, echo "xxx" is also followed by return
return is not followed by anything. . .
Why not use break here. . . . .
In what situations is return mostly used?
Please explain it in detail. Please, I am just getting started and I can’t understand a lot of functions. Please explain it in a simple and easy-to-understand way. . .
Functions generally have return. You can not write it. If you write it, you can also write return 1. In fact, there is still a slight difference.
For example
//saier function a(){ $b=1; return $b; } function b(){ $b=1; echo $b; } echo a(); // 输出1 b(); //输出1 //看起来都是输出1,而且b()好像还更方便,其实不然,如果要用到函数的返回值,b()就不行了 echo a()+1; //输出2 //b();这个就不能实现这种功能,需要进去函数里面改,扩展不方便
I have been unable to do it. I know how to use return but I don’t know when to use it and when not to use it. Can anyone tell me? Why do we need to add return in the following example?
money*0.8; } } $a=new man();echo $a->show();?>
return is to return a result to the caller of the function/method.
For example, in your example, calling the show() method will return aninteger(800). The place where it is called is equivalent to executing echo 800.
If your function/method is to perform some operations rather than return data, you do not need to return, but generally a Boolean value will be returned to indicate whether the operation is successful or failed so that theprocess can be carried out at the place where the call is made. control.
return, what is it after your function runs? If there is no return, your function will be empty after running.
For example, function a () {return 1;}
Execute a() and you will get 1
$b = a(); Then $b is equal to 1. If there is no return, then there will be no return value when executing a(), which is empty, then $b will be empty
The above is the detailed content of When to use return in php?. For more information, please follow other related articles on the PHP Chinese website!