How can functions in PHP return values to the calling code?
Functions in PHP can return values to the calling code using the `return` statement. When a function is called and it reaches a `return` statement, the function will stop executing and return the specified value to the calling code. This allows functions to pass data back to the code that called them, enabling the reuse of values calculated or processed within the function.
function calculateSum($num1, $num2) {
$sum = $num1 + $num2;
return $sum;
}
$result = calculateSum(5, 3);
echo $result; // Output: 8
Keywords
Related Questions
- What steps can be taken to troubleshoot and resolve problems with FCKeditor in PHP applications?
- How can you read multiple directories and count the total number of files in PHP?
- How can beginners improve their understanding of basic PHP and MySQL concepts to avoid common errors when working with databases?