Home » Php » php – quotation marks etc

php – quotation marks etc

Posted by: admin July 12, 2020 Leave a comment


Is there any different typing:

<?php echo $_SERVER[REQUEST_URI] ?>


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

and last

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

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


A Noob

How to&Answers:

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.


The first one is wrong – you’re actually looking for a constant REQUEST_URI that doesn’t exist. This will generate a notice-level warning.

There’s no difference between the other two.


There is a difference between single and double quotes in PHP string handling. A string enclosed in double quotes will be evaluated for embedded variables and escape characters (e.g. \n); a string enclosed in single quotes won’t (or not as much).

So, for example,

$hello = "world";

echo "Hello $hello!\n";
echo 'Hello $hello!\n';
echo 'Done';

will output

Hello world!
Hello $hello!\nDone

In situations where you have no escape characters or embedded variables, it is slightly more efficient to use single quotes as it requires less processing of the string by the runtime. However, many people (me included) prefer to use double quotes for all strings to save confusion.


As a caveat to Gumbo’s answer the third representation – double quotes – actually makes PHP look for variables inside that string. Thus that method might be a little slower (although in a string of 11 characters it’ll be negligible – it’s better practice not to make PHP do that however).


When PHP comes across plain strings being used as array keys it checks if there is a constant with that name and if there isn’t it defaults it back to an array key. Therefore, not using quote marks causes a slight performance hit and there is a possibility that the result will not be what you expect.



is syntatically incorrect and AFAIK will not run on a default installation of PHP5. The array index is a string so it needs to be passed on strings. I know PHP4 converted undefined constants to strings inside the square brackets but it’s still not good practice.

EDIT: Well unless you define a constant called REQUEST_URI, which you haven’t in your example script.


is the standard method and what you should be using.


also works and while not wrong is slightly more work for the PHP interpreter so unless you need to parse it for variables should not be used. (and if you need to do so, you need to rethink that part of your program.