Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New Coder
    Join Date
    Apr 2004
    Location
    Texas
    Posts
    60
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question use of quotes in array references

    I've noticed that all of the following work
    $_SERVER["PHP_SELF"]
    $_SERVER['PHP_SELF']
    $_SERVER[PHP_SELF]

    I've been using single quotes, but what is the convention?
    Last edited by black3842; 04-20-2004 at 08:44 AM.
    Jason B

  • #2
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    All of those should work, however I really dont reccomend the last one (no quotes)

    Generally, you will want to use the second one (single quotes), as for strings, single quotes are very slightly faster. On this small scale it wont make much (if any) difference, but its a good habbit to get into using double quotes only where needed.

  • #3
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    If the script is parsed, then values between single quotes are taken literally, so they are not parsed. This is the slight gain in processingtime.
    All text between double quotes is parsed.
    so if you have

    $test = 'bla';
    echo ('$test'); // prints $test
    echo ("$test"); // prints bla

    But there are hardly situations where you need double quotes. I only use it for sql-statements and sometimes code that i eval()
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #4
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Same here , SQL gets annoying if you have to escape all the single quotes.

  • #5
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That's an advantage though isn't it? It means you can go:
    Code:
    echo("Hello my name is $name and I live in $town");
    etc.
    "Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark

  • #6
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well, i'd personally use
    PHP Code:
    echo('Hello my name is ' $name ' and I live in ' $town); 
    Because i find it easier to read + with colourcoding, i can immedeately see what are strings and what are variables.
    Don't think there is a noticable perfomancediffernce, but for me it results in fewer errors.
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #7
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I would do the same as raf, I find it annoying trying to find variables in double quotes.

  • #8
    New Coder
    Join Date
    Apr 2004
    Location
    Texas
    Posts
    60
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks guys, yeah I do it the same as raf normally, for readability, just wasn't sure if arrays were handled differently....

    Thanks for the feedback.
    Jason B

  • #9
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,051
    Thanks
    10
    Thanked 94 Times in 92 Posts
    .. just one other thing which did not get mentioned, with no quotes at all you have possibly a whole new variable ...

    PHP Code:
    <?
    $glug 
    'grrrrrrr';
    DEFINE('glug','whataaaaa');
    $_SERVER['glug'] = 'blaaargh';
    DEFINE('PHP_SELF','glug');
    ?>
    without running it , what will the following print ?
    <?
    echo $_SERVER[PHP_SELF];
    echo 
    $_SERVER['PHP_SELF'];
    ?>
    in other words , given an unquoted argument to an array PHP looks first for a constant of that name , then if it fails it adds the quotes itself (thowing a warning) and looks for the quoted array variable.
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •