Are there best practices for structuring PHP code to improve readability and maintainability, especially when dealing with complex SQL queries and data manipulation?

When dealing with complex SQL queries and data manipulation in PHP, it is important to follow best practices to improve readability and maintainability. One way to achieve this is by separating your SQL queries from your PHP code using prepared statements and functions. This helps to keep your code organized and easier to understand.

// Example of structuring PHP code for complex SQL queries and data manipulation

// Function to execute a prepared statement with parameters
function executePreparedStatement($sql, $params) {
    $stmt = $pdo->prepare($sql);
    $stmt->execute($params);
    return $stmt;
}

// Example usage of the function
$sql = "SELECT * FROM users WHERE id = ?";
$params = [1];
$stmt = executePreparedStatement($sql, $params);

// Fetching data from the executed statement
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    // Process the data here
}