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
orif 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 <link>
'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 & 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.
$_SERVER['PHP_SELF']
matched one case while$_SERVER['PHP_URL_PATH']
matched a different one? \$\endgroup\$