What are some best practices for ensuring security in PHP code?

Issue: One common security vulnerability in PHP code is SQL injection, where malicious users can manipulate SQL queries to access or modify sensitive data in the database. To prevent SQL injection attacks, it is recommended to use prepared statements with parameterized queries instead of directly embedding user input into SQL queries. Code snippet:

```php
// Using prepared statements to prevent SQL injection
$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();
$result = $stmt->fetch();
```

In the code snippet above, we are using PDO (PHP Data Objects) to establish a database connection and prepare a SQL query with a placeholder `:username`. By binding the user input `$_POST['username']` to this parameter using `bindParam`, we are ensuring that the input is properly sanitized and preventing SQL injection attacks.