What best practices should be followed when setting up PHP scripts for handling user data, such as email addresses and names, to prevent potential data breaches?

When setting up PHP scripts for handling user data, it is important to follow best practices to prevent potential data breaches. One way to enhance security is to properly sanitize and validate user input to prevent SQL injection attacks. Additionally, using prepared statements with parameterized queries can help protect against malicious input. Lastly, storing sensitive data like passwords in hashed form using a secure hashing algorithm is crucial to safeguard user information.

// Sanitize and validate user input
$email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
$name = filter_var($_POST['name'], FILTER_SANITIZE_STRING);

// Prepare SQL statement with parameterized queries
$stmt = $pdo->prepare("INSERT INTO users (email, name) VALUES (:email, :name)");
$stmt->bindParam(':email', $email);
$stmt->bindParam(':name', $name);
$stmt->execute();

// Hash sensitive data like passwords
$password = password_hash($_POST['password'], PASSWORD_DEFAULT);