[ACCEPTED]-How to use $_SERVER['REQUEST_URI']-php
Without quotes PHP interprets the REQUEST_URI
as a constant but corrects your typo error if there is 10 no such constant and interprets it as string.
When 9 error_reporting
includes E_NOTICE
, you would probably get an error 8 such as:
Notice: Use of undefined constant 7 REQUEST_URI - assumed 'REQUEST_URI' in <file path> on 6 line <line number>
But if there is a constant with this 5 name, PHP will use the constant’s value 4 instead. (See also Array do's and don'ts)
So always use quotes when you mean a string. Otherwise it can have 3 unwanted side effects.
And for the difference 2 of single and double quoted strings, see 1 the PHP manual about strings.
The first one is wrong - you're actually 3 looking for a constant REQUEST_URI
that doesn't exist. This 2 will generate a notice-level warning.
There's 1 no difference between the other two.
There is a difference between single and 12 double quotes in PHP string handling. A 11 string enclosed in double quotes will be 10 evaluated for embedded variables and escape 9 characters (e.g. \n); a string enclosed 8 in single quotes won't (or not as much).
So, for 7 example,
$hello = "world";
echo "Hello $hello!\n";
echo 'Hello $hello!\n';
echo 'Done';
will output
Hello world!
Hello $hello!\nDone
In 6 situations where you have no escape characters 5 or embedded variables, it is slightly more 4 efficient to use single quotes as it requires 3 less processing of the string by the runtime. However, many 2 people (me included) prefer to use double 1 quotes for all strings to save confusion.
As a caveat to Gumbo's answer the third 6 representation - double quotes - actually 5 makes PHP look for variables inside that 4 string. Thus that method might be a little 3 slower (although in a string of 11 characters 2 it'll be negligible - it's better practice 1 not to make PHP do that however).
When PHP comes across plain strings being 6 used as array keys it checks if there is 5 a constant with that name and if there isn't 4 it defaults it back to an array key. Therefore, not 3 using quote marks causes a slight performance 2 hit and there is a possibility that the 1 result will not be what you expect.
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.