What are the potential pitfalls of not properly evaluating variables in PHP?

Not properly evaluating variables in PHP can lead to security vulnerabilities such as SQL injection or cross-site scripting attacks. It is crucial to always sanitize and validate user input to prevent these risks. One way to do this is by using functions like htmlentities() or mysqli_real_escape_string() to properly handle user input before using it in queries or outputting it to the browser.

// Example of properly evaluating and sanitizing user input
$userInput = $_POST['user_input'];
$cleanInput = htmlentities($userInput);

// Using the sanitized input in a query
$query = "SELECT * FROM users WHERE username = '" . mysqli_real_escape_string($cleanInput) . "'";
$result = mysqli_query($connection, $query);