Php if else not working with main root /

I have some php code that says if index.php or main root then display some code and if not then show other code and it works on index.php but if it’s just the domain name without index.php at the end, it don’t work. I have the following code at the moment

<?php $currentpage = $_SERVER['REQUEST_URI'];
if($currentpage=='/' || $currentpage=="/index.php" || $currentpage=="index.php" || $currentpage=="" ) {
?>

Can anyone help me out please as I don’t know what to amend for the code to work if main root and not have index.php at the end, the code should be the same for the main root / and index.php

Have you tried outputting what $_SERVER['REQUEST_URI']; is? It’ll tell you what you’re supposed to expect on page request.

Not yet, do I use echo to do that, sorry not great with some PHP

It looks like the code I have used above has worked, the server/site was just slow at updating

1 Like

I would suggest using something like Visual Studio Code to debug your code. You can step through what each variable contains. I think you can attach your PHP executable to VS Code to do that. Much easier than just sitting there and guessing.

Thank you, I’ll look into that and use that, I have been using brackets

Yes, you’d use either echo or var_dump() to show the value of a variable at a particular point in the code, perhaps with an exit() afterwards if there are issues with seeing the output on a crowded screen.

var_dump() is probably the better option, as it will work on any data type, not just strings. That will cover you for the unexpected, which is often what you get when de-bugging.

1 Like

Also keep in mind the URI carries any and all query parameters along with it, and in certain invocation contexts will also carry the scheme/domain information.