What are best practices for organizing PHP code to avoid long, unmanageable files with repeated code segments?

When organizing PHP code to avoid long, unmanageable files with repeated code segments, it is best to follow the principles of modularity and reusability. One effective way to achieve this is by using include or require statements to break up the code into separate files based on functionality or purpose. This helps in keeping the codebase organized, easier to maintain, and reduces the chances of repeating code segments.

// main.php
<?php
require 'header.php';
require 'functions.php';
require 'database.php';

// Rest of the code
?>

// header.php
<?php
// Header code here
?>

// functions.php
<?php
// Functions code here
?>

// database.php
<?php
// Database connection code here
?>