What best practices can be followed to avoid duplicate stylesheets inclusion in PHP modules?
To avoid duplicate stylesheets inclusion in PHP modules, it is recommended to use a centralized stylesheet inclusion mechanism. By creating a function that checks if a stylesheet has already been included before adding it to the page, you can prevent duplicates from being loaded.
function include_stylesheet($stylesheet) {
static $included_stylesheets = array();
if (!in_array($stylesheet, $included_stylesheets)) {
echo '<link rel="stylesheet" type="text/css" href="' . $stylesheet . '">';
$included_stylesheets[] = $stylesheet;
}
}
// Example of including stylesheets
include_stylesheet('styles/main.css');
include_stylesheet('styles/buttons.css');