What are the best practices for validating variables both in JavaScript and PHP?

Validating variables is crucial to ensure data integrity and prevent security vulnerabilities in both JavaScript and PHP. To validate variables, you can use built-in functions, regular expressions, or custom validation functions. It's important to sanitize user input to prevent SQL injection, XSS attacks, and other security threats. PHP code snippet for validating a variable using filter_var():

// Validate an email address
$email = "test@example.com";
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
    echo "Valid email address";
} else {
    echo "Invalid email address";
}