Introduction
Return values let a function send back a result after it finishes. This helps you reuse the result later in your program.
Return values let a function send back a result after it finishes. This helps you reuse the result later in your program.
<?php function functionName() { // code return value; } ?>
The return keyword sends a value back and stops the function.
After return, no code in the function runs.
<?php function add($a, $b) { return $a + $b; } ?>
<?php function greet() { return "Hello!"; } ?>
true if the number is even, otherwise false.<?php function isEven($num) { return $num % 2 === 0; } ?>
This program defines a function to multiply two numbers and returns the result. Then it prints the result.
<?php function multiply($x, $y) { return $x * $y; } $result = multiply(4, 5); echo "4 times 5 is " . $result . "\n"; ?>
Use return to send back any type of value: numbers, strings, booleans, or even arrays.
Once a function hits return, it stops running immediately.
Functions use return to send back results.
The returned value can be saved or used elsewhere.
Code after return inside a function does not run.
return statement do inside a PHP function?returnreturn statement sends a value back to the place where the function was called.return followed by the value and a semicolon: return 10;function add($a, $b) {
return $a + $b;
echo "Done";
}
$result = add(3, 4);
echo $result;return $a + $b; sends back 7 and stops the function, so echo "Done"; is never run.$result gets 7, and echo $result; prints 7 only.function multiply($x, $y) {
$product = $x * $y;
return
$product;
}return is on one line alone, so PHP treats it as returning nothing immediately.$product; is unreachable and not part of return, causing function to return null.null if none found?$str !== '' to find strings that are not empty exactly.null if no non-empty string is found, which is clear and intentional.Pass the test and this pattern moves unknown