What are some common pitfalls for beginners when writing PHP code?

One common pitfall for beginners when writing PHP code is not properly sanitizing user input, leaving the code vulnerable to security risks like SQL injection attacks. To solve this issue, always use prepared statements or parameterized queries when interacting with a database to prevent malicious input from being executed as SQL commands.

// Example of using prepared statements to sanitize user input in PHP
$pdo = new PDO('mysql:host=localhost;dbname=mydatabase', 'username', 'password');
$stmt = $pdo->prepare('SELECT * FROM users WHERE username = :username');
$stmt->bindParam(':username', $_POST['username']);
$stmt->execute();