Can you provide an example of correctly linking PHP elements such as function declarations and database queries in a separate file for better organization and maintenance of code?

To better organize and maintain code in PHP, it is recommended to separate function declarations and database queries into separate files. This separation allows for easier management of code, improves readability, and promotes reusability of functions and queries throughout the application.

// functions.php
<?php
function getUserById($id, $conn) {
    $sql = "SELECT * FROM users WHERE id = ?";
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("i", $id);
    $stmt->execute();
    $result = $stmt->get_result();
    return $result->fetch_assoc();
}
?>

// db_queries.php
<?php
function getAllUsers($conn) {
    $sql = "SELECT * FROM users";
    $result = $conn->query($sql);
    return $result->fetch_all(MYSQLI_ASSOC);
}
?>