Skip to content
Advertisement

How to use $_SERVER[‘REQUEST_URI’]

Is there any difference between typing:

<?php echo $_SERVER[REQUEST_URI] ?>

or

<?php echo $_SERVER['REQUEST_URI'] ?>

or

<?php echo $_SERVER["REQUEST_URI"] ?>

?

They all work… I use the first one. Maybe one is faster than the other?

Advertisement

Answer

Without quotes PHP interprets the REQUEST_URI as a constant but corrects your typo error if there is no such constant and interprets it as string.

When error_reporting includes E_NOTICE, you would probably get an error such as:

Notice: Use of undefined constant REQUEST_URI – assumed ‘REQUEST_URI’ in <file path> on line <line number>

But if there is a constant with this name, PHP will use the constant’s value instead. (See also Array do’s and don’ts)

So always use quotes when you mean a string. Otherwise it can have unwanted side effects.

And for the difference of single and double quoted strings, see the PHP manual about strings.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement