What are the potential pitfalls of not using functions in PHP for validation and error handling?
Not using functions for validation and error handling in PHP can lead to repetitive code, decreased readability, and difficulty in maintaining and updating the code. By using functions, you can encapsulate validation and error handling logic, making your code more modular and easier to manage.
// Function for validating email format
function validateEmail($email) {
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
return false;
}
return true;
}
// Example usage
$email = "example@example.com";
if (!validateEmail($email)) {
echo "Invalid email format";
}