New course! Every coder should learn Generative AI!
Try a free lesson0
What is the difference between these 2 codes
// code1 <?php function mult($num1, $num2) { $res = $num1 * $num2; echo $res; } mult(8, 3); ?> // code2 <?php function mult($num1, $num2) { $res = $num1 * $num2; return $res; } echo mult(8, 3); ?> what is the point of "return" ?!
2 Answers
+ 2
returns gives you the value. The result of the function.
So with the return mult, you can store it in other variable, or even do
$res = mult(mult(2,2),2) so the inside mult will be 4, and because it was returned, the expresion goes to mult(4,2).
so $res is 8.
0
in code2 the function returns the result to the point of call. so you can print outside the function, or do other things with that result value.