What are the best practices for passing variables as function parameters in PHP?
When passing variables as function parameters in PHP, it is best practice to ensure that the variables are properly sanitized and validated to prevent security vulnerabilities such as SQL injection or cross-site scripting attacks. Additionally, it is recommended to use type hinting to specify the data type of the parameter for better code readability and error handling.
// Example of passing a variable as a function parameter with type hinting
function calculateTotal(int $quantity, float $price) {
$total = $quantity * $price;
return $total;
}
$quantity = 5;
$price = 10.99;
echo calculateTotal($quantity, $price); // Output: 54.95