What are the potential pitfalls of directly including the database connection within each function in PHP?

Including the database connection within each function in PHP can lead to code duplication, making it harder to maintain and update the code in the future. It also violates the principle of separation of concerns, as the database connection logic should ideally be centralized. To solve this issue, it is recommended to create a separate file for the database connection and include it in the necessary files or functions.

// db_connection.php
<?php

$host = 'localhost';
$dbname = 'database_name';
$username = 'username';
$password = 'password';

$connection = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
```

```php
// function.php
<?php

include 'db_connection.php';

function getDataFromDatabase() {
    global $connection;
    
    // function logic here
}

function insertDataIntoDatabase() {
    global $connection;
    
    // function logic here
}