How can PHP handle special characters like apostrophes in form inputs to prevent escaping?
Special characters like apostrophes can be handled in PHP by using prepared statements with parameterized queries when interacting with databases to prevent SQL injection. Additionally, htmlspecialchars() function can be used to convert special characters to HTML entities to prevent XSS attacks when displaying user input on a webpage.
// Example of handling special characters in form input
$input = $_POST['input']; // Assuming 'input' is the name of the form field
// Prevent SQL injection using prepared statements
$stmt = $pdo->prepare("INSERT INTO table (column) VALUES (:input)");
$stmt->bindParam(':input', $input);
$stmt->execute();
// Prevent XSS attacks by converting special characters to HTML entities
echo htmlspecialchars($input);