What are some common mistakes to avoid when using echo in PHP?

One common mistake to avoid when using echo in PHP is not properly escaping special characters, which can lead to security vulnerabilities like cross-site scripting (XSS) attacks. To prevent this, always use htmlspecialchars() function to escape any user input before echoing it to the browser.

// Incorrect way: not escaping special characters
$userInput = "<script>alert('XSS attack');</script>";
echo $userInput;

// Correct way: escaping special characters
$userInput = "<script>alert('XSS attack');</script>";
echo htmlspecialchars($userInput);