Dynamically loading CSS files based on URL or URI in PHP I have written out the following code in the head section of my document,
which should load 3 separate style sheets for each page based on the URL/URI that the user is visiting. It is working as intended as tested with the comment in the code but I am wondering if there is a more efficient way to do this. I initially started writing out a switch statement, but chose to try if else statements before.

Also I have added this part:

 || $_SERVER['PHP_URL_PATH']


to each statement in case there is an error of some kind with the first expression. It seems to be working whether I use just ['PHP_URL_PATH']
or the full $_SERVER['PHP_URL_PATH'].

My questions are:


Which out of the two would be more efficient (switch or if else)?
Is the second declaration of $_SERVER actually necessary, or will it work without this specificity? 


Any improvements that people could point me to would be greatly appreciated. 

PS: I have removed all the echo's for including the actually &lt;link&gt;'s to the CSS files.

This has been tested and is showing to work in Firefox Developer Edition so guessing it's ok, but could be improved maybe; what about scalability, or any security concerns? I am new so please advise or help.

// Create Logic here to include various different style sheets. 

if($_SERVER['PHP_SELF'] || $_SERVER['PHP_URL_PATH']  === "/mainHubV8.1.php"){
    echo "Loading Styles for MainHubV8.1.php";

    }
else if ($_SERVER['PHP_SELF'] || $_SERVER['PHP_URL_PATH'] === "/advSearchV8.1.php"){
    echo "Loading Styles for advSearchV8.1.php";

    }
else if ($_SERVER['PHP_SELF'] || $_SERVER['PHP_URL_PATH'] === "/loginOrSignUpV8.1.php"){
    echo "Loading Styles for loginOrSignUpV8.1.php";

    }
else if ($_SERVER['PHP_SELF'] || $_SERVER['PHP_URL_PATH'] === "/profilePageV8.1.php"){
    echo "Loading Styles for profilePageV8.1.php";

    }                            
else if ($_SERVER['PHP_SELF'] || $_SERVER['PHP_URL_PATH'] === "/chatApllicationV8.1.php"){
    echo "Loading Styles for chatApllicationV8.1.php";

    }                            
else if ($_SERVER['PHP_SELF'] || $_SERVER['PHP_URL_PATH'] === "/onlineUsersV8.1.php"){
    echo "Loading Styles for onlineUsersV8.1.php";

    }                            
else if ($_SERVER['PHP_SELF'] || $_SERVER['PHP_URL_PATH'] === "/index.php"){
    echo "Loading Styles for index.php";

    }                            
else if ($_SERVER['PHP_SELF'] || $_SERVER['PHP_URL_PATH'] === "/404"){
    echo "Loading Styles for error page";

    }


Is there a better alternative to achieving the same goal? If so could you please provide reference or articles/Question &amp; answers anywhere on Stack Exchange or some other resource. Plus I do not want to use Javascript or JQuery really as these can be turned off and disabled. So PHP seems more appropriate.
Most of questions you are asking are not fit for this site and your code does not work as intended but this is an interesting case to review
The grave mistake
Adding anything to your code just in case there would be some imaginary &quot;error of some kind&quot; is the worst thing a programmer could do ever. You effectively ruined your code with adding that || $_SERVER['PHP_URL_PATH'] stuff:

there is no such thing $_SERVER['PHP_URL_PATH'] for starter: such a variable just doesn't exist
of course ['PHP_URL_PATH'] without the $_SERVER part makes no sense
neither the whole condition returns anything sensible due to PHP syntax rules you are yet to learn.

Yet this code works somehow, albeit not the way you expect.
The operator precedence
The way this code executes is a very interesting matter, once I wrote an article that explains it in detail, Operator precedence or how does 'or die()' work. Given you only started learning it could be too complex for you for the moment to wrap your head around, but in time you will find it very interesting to read.
$_SERVER['PHP_SELF'] || $_SERVER['PHP_URL_PATH']  === &quot;/mainHubV8.1.php&quot; doesn't mean &quot;if either PHP_SELF or PHP_URL_PATH equal to /mainHubV8.1.php&quot;. It means &quot;if PHP_SELF contains any value OR PHP_URL_PATH is equal to /mainHubV8.1.php&quot;.
In short, an expression $_SERVER['PHP_SELF'] || $_SERVER['PHP_URL_PATH']  === &quot;/mainHubV8.1.php&quot; evaluates to true when $_SERVER['PHP_SELF'] contains any value. Which means always.
Given PHP already has it's answer, it ceases to execute the further condition. This is why you can write either $_SERVER['PHP_URL_PATH'] or ['PHP_URL_PATH'] or don't even get a notice for the non-existent array index - this code is just never executed.
As a result, this code will always output just &quot;Loading Styles for MainHubV8.1.php&quot; no matter which page it is called on.
The right way
To elaborate on the Neoan's answer, given you have 3 styles to load, here is the proposed array structure
$stylesheets = [
    '/mainHubV8.1.php' =&gt; [
        'style1.css',
        'style2.css',
        'style3.css',
    ],
    '/advSearchV8.1.php' =&gt; [
        'style4.css',
        'style5.css',
        'style6.css',
    ],
    // and so on
];

given this structure, you will be able to get the correct styles right from $_SERVER['PHP_SELF']:
foreach($stylesheets[$_SERVER['PHP_SELF']] as $file) {
    echo &quot;&lt;link rel='stylesheet' type='text/css' href='$file'&gt;\n&quot;;
}