What are some best practices for organizing PHP code to separate database connections from HTML output?

To separate database connections from HTML output in PHP code, it is best practice to follow the MVC (Model-View-Controller) design pattern. This involves creating separate files for database connection logic (Model), processing data and business logic (Controller), and displaying the output (View). By organizing code in this way, it allows for better code organization, reusability, and maintainability.

// Model (db_connection.php)
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
?>

// Controller (data_processing.php)
<?php
include 'db_connection.php';

// Data processing logic here
?>

// View (output_display.php)
<?php
include 'data_processing.php';

// HTML output display here
?>