Marzikula
Marzikula

Reputation:

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?

Upvotes: 10

Views: 14631

Answers (6)

wdthm
wdthm

Reputation: 37

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.

Upvotes: 1

Macha
Macha

Reputation: 14664

$_SERVER[REQUEST_URI]

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.

$_SERVER['REQUEST_URI']

is the standard method and what you should be using.

$_SERVER["REQUEST_URI"]

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.

Upvotes: -1

Gumbo
Gumbo

Reputation: 655679

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.

Upvotes: 22

Mark Rendle
Mark Rendle

Reputation: 9414

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.

Upvotes: 3

Ross
Ross

Reputation: 47057

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).

Upvotes: 1

Greg
Greg

Reputation: 321796

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.

Upvotes: 3

Related Questions