www.webdeveloper.com
Results 1 to 7 of 7

Thread: PHP syntax -- referencing an assoc. array

  1. #1
    Join Date
    Jun 2009
    Posts
    20

    PHP syntax -- referencing an assoc. array

    My question has to do with whether or not the syntax I am suing is "valid" php syntax, i.e. is it standard practice? will it be supported in future versions of php?

    I am using php 5.3.8.

    Given this test code below, is the reference to $a['elt2'] valid in the <input> tag value="$a[elt"]>? That is, no quotes around the key, elt1.
    <?php

    $a = array();
    $a['elt1'] = "value of elt1";
    $a['elt2'] = "value of elt2";

    echo "a['elt'] = ", $a['elt1'], ". This is the usual way to reference the value, i.e key in quotes. <br />";

    echo <<<PRINT_INPUT
    <input type="text" size="150" name="demo"
    value="a['elt2'] = $a[elt2]. Is this way of referencing the value ok, i.e. no quotes around the key?" />
    PRINT_INPUT;
    ?>

    Thanks for your help.
    Andynic

  2. #2
    Join Date
    Jun 2009
    Posts
    20
    Correction: That question should read:
    Given this test code below, is the reference to $a['elt2'] valid in the <input> tag value="$a[elt1]">? That is, no quotes around the key, elt1.

  3. #3
    Join Date
    Jun 2009
    Posts
    20
    Another correction:
    Given this test code below, is the reference to $a['elt1'] valid in the <input> tag value="$a[elt1]">? That is, no quotes around the key, elt1.

  4. #4
    Join Date
    Jul 2003
    Location
    The City of Roses
    Posts
    2,503
    for(split(//,'))*))91:+9.*4:1A1+9,1))2*:..)))2*:31.-1)4131)1))2*:3)"'))
    {for(ord){$i+=$_&7;grep(vec($s,$i++,1)=1,1..($_>>3)-4);}}print"$s\n";

  5. #5
    Join Date
    Jun 2009
    Posts
    20
    Thanks for the reply, Jeff. That link to the php site is very good.
    Appreciate the help.
    Andynic

  6. #6
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    18,923
    Note that for the parser to correctly deal with a quoted array key (which is a good practice for consistency's sake), you cannot just use it as such within a double-quoted string. You will either have to use "complex" notation...
    PHP Code:
    echo "<input type='text' name='foo' value='{$array['bar']}' />"
    ...or else concatentation...
    PHP Code:
    echo "<input type='text' name='foo' value='" $array['bar'] ."' />"
    (Or use other options such as printf().)
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  7. #7
    Join Date
    Jun 2009
    Posts
    20
    Hi NogDog,
    Thanks for the succinct and accurate reply.
    Both work.
    Andynic

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles