Quotation marks etc.

Is there a different typing:

<?php echo $_SERVER[REQUEST_URI] ?> 

and

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

and last

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

They all work ... I use the first one. Is one faster than the other?

thanks

A noob

+7
php
source share
6 answers

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

When error_reporting enables E_NOTICE , you will probably get an error, for example:

Note: using the undefined constant REQUEST_URI - it is assumed that 'REQUEST_URI' is in <file path> on line <line number>

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

Therefore, always use quotation marks when you mean a string. Otherwise, unwanted side effects may occur.

And for the difference between single and double string quotes, see the PHP manual for strings .

+16
source share

The first one is wrong - you are really looking for a REQUEST_URI constant that does not exist. This will result in a warning at the notification level.

There is no difference between the other two.

+3
source share

In PHP string processing, there is a difference between single and double quotes. A double-quoted string will be evaluated for inline variables and escape characters (for example, \ n); a string enclosed in single quotes will not (or not).

So for example

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

displays

Hello World!
Hello, hello! \ NStart

In situations where you do not have escape characters or built-in variables, it is somewhat more efficient to use single quotes, since less string processing is required at runtime. However, many people (including me) prefer to use double quotes for all strings in order to save confusion.

+2
source share

As a warning to Gumbo, respond to the third view - double quotes - actually causes PHP to look for variables inside this line. Thus, this method can be a little slower (although in a line of 11 characters this will be insignificant - it is better to practice so that PHP does not do this).

+1
source share

When PHP encounters simple strings used as array keys, it checks to see if there is a constant with that name, and if not, it will return it to the array key by default. Therefore, without using quotation marks, you get a slight increase in performance, and there is a chance that the result will not be what you expect.

+1
source share
 $_SERVER[REQUEST_URI] 

is syntactically incorrect, and AFAIK will not start when PHP5 is installed by default. The array index is a string, so it must be passed in rows. I know that PHP4 converted undefined constants to strings in square brackets, but this is still not a good practice.

EDIT: Well, if you do not define a constant named REQUEST_URI, which you do not have in your example script.

 $_SERVER['REQUEST_URI'] 

is the standard method and what you should use.

 $_SERVER["REQUEST_URI"] 

also works and, although not mistaken, works a little more for the PHP interpreter, so if you do not need to parse it, variables should not be used. (and if you need to do this, you need to rethink that part of your program.

-one
source share

All Articles