Skip to main content

I'm not sure why you're handling so many CSS files. If you have one CSS file for your application, which is minified and cached on the user's device, you can reduce HTTP requests and overall loading time.

Anyway. If you need to handle lots of these files and keep this approach, you could simplify your code in terms of readability.


Create an array of default CSS files:

$css = ['a', 'b', 'c'];

Create an array where you store the individual files for each page:

$pageCSS = [
    'page-a' => [
        'add' => ['d', 'e', 'f'],
        'remove' => [],
    ],
    'page-b' => [
        'add' => ['g', 'h', 'i'],
        'remove' => ['a', 'b'],
    ],
];

Now you need to test whether a requested page is in this array. If the key exists, add all new files and remove not-needed default files using array_mergearray_merge and array_diffarray_diff:

$page = 'page-a';

if (array_key_exists($page, $pageCSS)) {
    $css = array_diff(array_merge($css, $pageCSS[$page]['add']), $pageCSS[$page]['remove']);
}

Finally create all HTML elements. I find it easier to read, if you use single-quotes. This way you don't need to escape all double quotes with backslashes:

foreach($css as $file) {
    print '<link rel="stylesheet" type="text/css" href="' . $file . '.css">';
}

I'm not sure why you're handling so many CSS files. If you have one CSS file for your application, which is minified and cached on the user's device, you can reduce HTTP requests and overall loading time.

Anyway. If you need to handle lots of these files and keep this approach, you could simplify your code in terms of readability.


Create an array of default CSS files:

$css = ['a', 'b', 'c'];

Create an array where you store the individual files for each page:

$pageCSS = [
    'page-a' => [
        'add' => ['d', 'e', 'f'],
        'remove' => [],
    ],
    'page-b' => [
        'add' => ['g', 'h', 'i'],
        'remove' => ['a', 'b'],
    ],
];

Now you need to test whether a requested page is in this array. If the key exists, add all new files and remove not-needed default files using array_merge and array_diff:

$page = 'page-a';

if (array_key_exists($page, $pageCSS)) {
    $css = array_diff(array_merge($css, $pageCSS[$page]['add']), $pageCSS[$page]['remove']);
}

Finally create all HTML elements. I find it easier to read, if you use single-quotes. This way you don't need to escape all double quotes with backslashes:

foreach($css as $file) {
    print '<link rel="stylesheet" type="text/css" href="' . $file . '.css">';
}

I'm not sure why you're handling so many CSS files. If you have one CSS file for your application, which is minified and cached on the user's device, you can reduce HTTP requests and overall loading time.

Anyway. If you need to handle lots of these files and keep this approach, you could simplify your code in terms of readability.


Create an array of default CSS files:

$css = ['a', 'b', 'c'];

Create an array where you store the individual files for each page:

$pageCSS = [
    'page-a' => [
        'add' => ['d', 'e', 'f'],
        'remove' => [],
    ],
    'page-b' => [
        'add' => ['g', 'h', 'i'],
        'remove' => ['a', 'b'],
    ],
];

Now you need to test whether a requested page is in this array. If the key exists, add all new files and remove not-needed default files using array_merge and array_diff:

$page = 'page-a';

if (array_key_exists($page, $pageCSS)) {
    $css = array_diff(array_merge($css, $pageCSS[$page]['add']), $pageCSS[$page]['remove']);
}

Finally create all HTML elements. I find it easier to read, if you use single-quotes. This way you don't need to escape all double quotes with backslashes:

foreach($css as $file) {
    print '<link rel="stylesheet" type="text/css" href="' . $file . '.css">';
}
Source Link

I'm not sure why you're handling so many CSS files. If you have one CSS file for your application, which is minified and cached on the user's device, you can reduce HTTP requests and overall loading time.

Anyway. If you need to handle lots of these files and keep this approach, you could simplify your code in terms of readability.


Create an array of default CSS files:

$css = ['a', 'b', 'c'];

Create an array where you store the individual files for each page:

$pageCSS = [
    'page-a' => [
        'add' => ['d', 'e', 'f'],
        'remove' => [],
    ],
    'page-b' => [
        'add' => ['g', 'h', 'i'],
        'remove' => ['a', 'b'],
    ],
];

Now you need to test whether a requested page is in this array. If the key exists, add all new files and remove not-needed default files using array_merge and array_diff:

$page = 'page-a';

if (array_key_exists($page, $pageCSS)) {
    $css = array_diff(array_merge($css, $pageCSS[$page]['add']), $pageCSS[$page]['remove']);
}

Finally create all HTML elements. I find it easier to read, if you use single-quotes. This way you don't need to escape all double quotes with backslashes:

foreach($css as $file) {
    print '<link rel="stylesheet" type="text/css" href="' . $file . '.css">';
}