What are some best practices for separating presentation, data, and logic in PHP code?

To separate presentation, data, and logic in PHP code, it is recommended to use a design pattern such as Model-View-Controller (MVC). This involves creating separate files or directories for each component: models for data handling, views for presentation, and controllers for logic. This separation helps to improve code organization, readability, and maintainability.

// Example of separating presentation, data, and logic using MVC pattern

// Model (data handling)
class User {
    public function getUserById($id) {
        // Database query to fetch user data
        return $userData;
    }
}

// View (presentation)
class UserView {
    public function displayUser($userData) {
        // HTML output to display user data
    }
}

// Controller (logic)
class UserController {
    public function showUser($id) {
        $userModel = new User();
        $userData = $userModel->getUserById($id);

        $userView = new UserView();
        $userView->displayUser($userData);
    }
}

// Implementation
$userController = new UserController();
$userController->showUser($userId);