What is the best practice for passing variables between functions in PHP?
When passing variables between functions in PHP, the best practice is to use arguments and return values. This ensures that functions are self-contained and do not rely on global variables, making the code more modular and easier to maintain.
// Function to calculate the square of a number
function calculateSquare($num) {
return $num * $num;
}
// Function to display the result
function displayResult($result) {
echo "The result is: " . $result;
}
// Usage
$number = 5;
$square = calculateSquare($number);
displayResult($square);