What are common pitfalls to avoid when starting to work with PHP?

One common pitfall to avoid when starting to work with PHP is not properly sanitizing user input, which can leave your application vulnerable to security risks such as SQL injection attacks. To solve this, always use prepared statements or input validation functions to sanitize user input before using it in your code.

// Example of using prepared statements to sanitize user input
$stmt = $pdo->prepare('SELECT * FROM users WHERE username = :username');
$stmt->bindParam(':username', $_POST['username']);
$stmt->execute();
```

Another common pitfall is not handling errors effectively, which can lead to unexpected behavior or security vulnerabilities in your application. To solve this, always use error handling techniques such as try-catch blocks or logging errors to a file.

```php
// Example of using try-catch blocks to handle errors
try {
    // code that may throw an exception
} catch (Exception $e) {
    echo 'An error occurred: ' . $e->getMessage();
}
```

Lastly, a common pitfall is not optimizing your code for performance, which can lead to slow loading times and inefficient use of server resources. To solve this, always use best practices such as caching, optimizing database queries, and minimizing the use of resource-intensive functions.

```php
// Example of optimizing database queries by using indexes
$stmt = $pdo->prepare('SELECT * FROM users WHERE username = :username');
$stmt->bindParam(':username', $_POST['username']);
$stmt->execute();