SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Enthusiast thechronic's Avatar
    Join Date
    Jun 2002
    Posts
    64
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Using associative array inside double quotes

    I would like to use the following code for a MySQL query:
    PHP Code:
    $query "SELECT * FROM table WHERE item0 = $row['ID'] AND item1 = 0"
    However I get the following parse error:
    parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING'
    What is wrong here, is it perhaps not possible to use associative arrays inside double quotes?

    When I use $row instead of $row['ID'] the code works fine. I can ofcourse use
    PHP Code:
    $query "SELECT * FROM table WHERE item0 = ".$row['ID']." AND item1 = 0"
    but I'm trying to figure out what the possibilities and limitations are using variables inside double quotes.

  2. #2
    Probably eating pie mitsubishi's Avatar
    Join Date
    Sep 2001
    Location
    England, UK
    Posts
    405
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You need braces:
    PHP Code:
    $query "SELECT * FROM table WHERE item0 = {$row['ID']} AND item1 = 0"

  3. #3
    SitePoint Enthusiast thechronic's Avatar
    Join Date
    Jun 2002
    Posts
    64
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wow I didn't expect that, but it works fine.

    Thanks to the mentioning of curly braces I finally found the complete documentation on the use of single and double quotes
    http://www.php.net/manual/en/language.types.string.php

    Thanks Mitsu!!!!

  4. #4
    Hi there! Owen's Avatar
    Join Date
    Jan 2000
    Location
    CA
    Posts
    1,165
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Or (for the lazy programmer)

    PHP Code:
    $query "SELECT * FROM table WHERE item0 = $row[ID] AND item1 = 0"
    Owen

  5. #5
    SitePoint Enthusiast thechronic's Avatar
    Join Date
    Jun 2002
    Posts
    64
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is that legal?

  6. #6
    SitePoint Wizard Mincer's Avatar
    Join Date
    Mar 2001
    Location
    London | UK
    Posts
    1,140
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by thechronic
    Is that legal?
    Yes.

    Personally I prefer to break out of the string for arrays, or better still assign a single variable prior to constructing the query, it makes the code easier to read IMHO.

  7. #7
    SitePoint Wizard silver trophy Karl's Avatar
    Join Date
    Jul 1999
    Location
    Derbyshire, UK
    Posts
    4,411
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually doign $array[id] etc. isn't legal, if you increase the error reporting a notch it'll tell you that you're using an undefined constant (id) in place of a string, it's just that PHP takes the name of the undefined constant and uses it as a string.
    Karl Austin :: Profile :: KDA Web Services Ltd.
    Business Web Hosting :: Managed Dedicated Hosting
    Call 0800 542 9764 today and ask how we can help your business grow.

  8. #8
    SitePoint Wizard Mincer's Avatar
    Join Date
    Mar 2001
    Location
    London | UK
    Posts
    1,140
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, I'll retract that, I didn't realise that php had chnaged their coding practice recommendations.

  9. #9
    Probably eating pie mitsubishi's Avatar
    Join Date
    Sep 2001
    Location
    England, UK
    Posts
    405
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by Karl
    Actually doign $array[id] etc. isn't legal, if you increase the error reporting a notch it'll tell you that you're using an undefined constant (id) in place of a string, it's just that PHP takes the name of the undefined constant and uses it as a string.
    This would normally be true, but not in double quoted strings, you would have to have the braces around for PHP to accept it as a constant, e.g.
    PHP Code:
    <?php
    error_reporting
    (E_ALL);
    $row[2] = "goodbye";
    $row['ID'] = "hello";
    echo 
    "The word is $row[ID] <br />";
    define('ID'2);
    echo 
    "The word is $row[ID] <br />";
    echo 
    "The word is {$row[ID]} <br />";
    ?>

  10. #10
    Hi there! Owen's Avatar
    Join Date
    Jan 2000
    Location
    CA
    Posts
    1,165
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Edit: Oops. Nevermind.

    Owen


Bookmarks

Posting Permissions

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