Results 1 to 3 of 3

Thread: String Interpolation

  1. #1
    Join Date
    Apr 2012
    Location
    Central New Jersey
    Posts
    286
    Thanks
    95
    Thanked 3 Times in 3 Posts

    Default String Interpolation

    This works:

    echo $quotations [4];
    echo "<br />";
    echo $quotations [5];
    echo "<br />";
    echo "The PHP End";


    This doesn't:

    echo "$quotations [4] <br /><br /> $quotations [5] <br /><br />";



    Am I misunderstanding string interpolation?

    A.

  2. #2
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    PHP Code:
    echo "$quotations [4] <br /><br /> $quotations [5] <br /><br />"
    PHP doesn't realize that the indices are part of the variables - it thinks they're part of the string.
    You can resolve the ambiguity by using brackets:
    PHP Code:
    echo "{$quotations[4]}<br>{$quotations[5]}<br>"
    (Incidentally, see how you can tell what's wrong by looking at the colors in the code highlighting? When you code, use an editor that does that. very helpful.)

  3. The Following User Says Thank You to traq For This Useful Post:

    marain (05-13-2012)

  4. #3
    Join Date
    Apr 2012
    Location
    Chester, Cheshire
    Posts
    329
    Thanks
    7
    Thanked 35 Times in 35 Posts

    Default

    Quote Originally Posted by traq View Post
    (Incidentally, see how you can tell what's wrong by looking at the colors in the code highlighting? When you code, use an editor that does that. very helpful.)
    http://notepad-plus-plus.org/

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
  •