Interpolation (double quoted string) of Associative Arrays in PHP












43















When interpolating PHP's string-indexed array elements (5.3.3, Win32)
the following behavior may be expected or not:



$ha = array('key1' => 'Hello to me');

print $ha['key1']; # correct (usual way)
print $ha[key1]; # Warning, works (use of undefined constant)

print "He said {$ha['key1']}"; # correct (usual way)
print "He said {$ha[key1]}"; # Warning, works (use of undefined constant)

print "He said $ha['key1']"; # Error, unexpected T_ENCAPSED_AND_WHITESPACE
print "He said $ha[ key1 ]"; # Error, unexpected T_ENCAPSED_AND_WHITESPACE
print "He said $ha[key1]"; # !! correct (How Comes?)


Inerestingly, the last line seems to be correct PHP code. Any explanations?
Can this feature be trusted?






Edit: The point of the posting now set in bold face in order to reduce misunderstandings.








share|improve this question

























  • See also: stackoverflow.com/questions/27742321/…

    – dreftymac
    Jul 27 '18 at 21:44
















43















When interpolating PHP's string-indexed array elements (5.3.3, Win32)
the following behavior may be expected or not:



$ha = array('key1' => 'Hello to me');

print $ha['key1']; # correct (usual way)
print $ha[key1]; # Warning, works (use of undefined constant)

print "He said {$ha['key1']}"; # correct (usual way)
print "He said {$ha[key1]}"; # Warning, works (use of undefined constant)

print "He said $ha['key1']"; # Error, unexpected T_ENCAPSED_AND_WHITESPACE
print "He said $ha[ key1 ]"; # Error, unexpected T_ENCAPSED_AND_WHITESPACE
print "He said $ha[key1]"; # !! correct (How Comes?)


Inerestingly, the last line seems to be correct PHP code. Any explanations?
Can this feature be trusted?






Edit: The point of the posting now set in bold face in order to reduce misunderstandings.








share|improve this question

























  • See also: stackoverflow.com/questions/27742321/…

    – dreftymac
    Jul 27 '18 at 21:44














43












43








43


9






When interpolating PHP's string-indexed array elements (5.3.3, Win32)
the following behavior may be expected or not:



$ha = array('key1' => 'Hello to me');

print $ha['key1']; # correct (usual way)
print $ha[key1]; # Warning, works (use of undefined constant)

print "He said {$ha['key1']}"; # correct (usual way)
print "He said {$ha[key1]}"; # Warning, works (use of undefined constant)

print "He said $ha['key1']"; # Error, unexpected T_ENCAPSED_AND_WHITESPACE
print "He said $ha[ key1 ]"; # Error, unexpected T_ENCAPSED_AND_WHITESPACE
print "He said $ha[key1]"; # !! correct (How Comes?)


Inerestingly, the last line seems to be correct PHP code. Any explanations?
Can this feature be trusted?






Edit: The point of the posting now set in bold face in order to reduce misunderstandings.








share|improve this question
















When interpolating PHP's string-indexed array elements (5.3.3, Win32)
the following behavior may be expected or not:



$ha = array('key1' => 'Hello to me');

print $ha['key1']; # correct (usual way)
print $ha[key1]; # Warning, works (use of undefined constant)

print "He said {$ha['key1']}"; # correct (usual way)
print "He said {$ha[key1]}"; # Warning, works (use of undefined constant)

print "He said $ha['key1']"; # Error, unexpected T_ENCAPSED_AND_WHITESPACE
print "He said $ha[ key1 ]"; # Error, unexpected T_ENCAPSED_AND_WHITESPACE
print "He said $ha[key1]"; # !! correct (How Comes?)


Inerestingly, the last line seems to be correct PHP code. Any explanations?
Can this feature be trusted?






Edit: The point of the posting now set in bold face in order to reduce misunderstandings.





php associative-array






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 9 '13 at 11:14









Rais Alam

5,728114784




5,728114784










asked Jan 19 '11 at 17:59









rubber bootsrubber boots

11.4k52844




11.4k52844













  • See also: stackoverflow.com/questions/27742321/…

    – dreftymac
    Jul 27 '18 at 21:44



















  • See also: stackoverflow.com/questions/27742321/…

    – dreftymac
    Jul 27 '18 at 21:44

















See also: stackoverflow.com/questions/27742321/…

– dreftymac
Jul 27 '18 at 21:44





See also: stackoverflow.com/questions/27742321/…

– dreftymac
Jul 27 '18 at 21:44












4 Answers
4






active

oldest

votes


















45














Yes, you may trust it. All ways of interpolation a variable are covered in the documentation pretty well.



If you want to have a reason why this was done so, well, I can't help you there. But as always: PHP is old and has evolved a lot, thus introducing inconsistent syntax.






share|improve this answer


























  • @nikic, I can't find this exact case (w/o curly braces) in the docs, where is it? Thanks, rbo

    – rubber boots
    Jan 19 '11 at 18:11













  • @mario: Personally I think it's not good, but many probably others are okay with it -> Dropped that part.

    – NikiC
    Jan 19 '11 at 18:12











  • @rubber boots: look out for this line: echo "He drank some $juices[koolaid1] juice.".PHP_EOL;.

    – NikiC
    Jan 19 '11 at 18:13






  • 2





    @mario, I come from Perl (still live there) and interpolation in Perl usually makes things more readable. The DMF in PHP here (dearly mising feature) is Perls q-operator (quote) series -> qq{}, q{}, qx{}, qw{}. Therefore (imho) string interpolation might be not as useful in PHP as it could be. :-(

    – rubber boots
    Jan 19 '11 at 18:20






  • 1





    @nikic somehow serve, right. But not on a single line. compare Perl: $x = qq{<a href="$link">$text</a>};

    – rubber boots
    Jan 19 '11 at 18:40





















10














Yes, this is well defined behavior, and will always look for the string key 'key', and not the value of the (potentially undefined) constant key.



For example, consider the following code:



$arr = array('key' => 'val');
define('key', 'defined constant');
echo "$arr[key] within string is: $arr[key]";


This will output the following:



$arr[key] within string is: val


That said, it's probably not best practice to write code like this, and instead either use:



$string = "foo {$arr['key']}"


or



$string = 'foo ' . $arr['key']


syntax.






share|improve this answer

































    9














    The last one is a special case handled by the PHP tokenizer. It does not look up if any constant by that name was defined, it always assumes a string literal for compatibility with PHP3 and PHP4.






    share|improve this answer



















    • 5





      Just for those interested (probably nobody...): PHP will generate a T_STRING for the array index (or a T_NUM_STRING if it is a non-overflowing decimal number) instead of the normal T_CONSTANT_ENCAPSED_STRING.

      – NikiC
      Jan 19 '11 at 18:18



















    0














    To answer your question, yes, yes it can, and much like implode and explode, php is very very forgiving... so inconsistency abound



    And I have to say I like PHP's interpolation for basical daisy punching variables into strings then and there,



    However if your doing only string variable interpolation using a single array's objects, it may be easier to write a template which you can daisy print a specific object variables into (like in say javascript or python) and hence explicit control over the variable scope and object being applied to the string



    I though this guy's isprintf really useful for this kind of thing



    http://www.frenck.nl/2013/06/string-interpolation-in-php.html



    <?php

    $values = array(
    'who' => 'me honey and me',
    'where' => 'Underneath the mango tree',
    'what' => 'moon',
    );

    echo isprintf('%(where)s, %(who)s can watch for the %(what)s', $values);

    // Outputs: Underneath the mango tree, me honey and me can watch for the moon





    share|improve this answer



















    • 1





      Link reports a 404 error

      – fpierrat
      Sep 29 '15 at 10:48











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f4738850%2finterpolation-double-quoted-string-of-associative-arrays-in-php%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    4 Answers
    4






    active

    oldest

    votes








    4 Answers
    4






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    45














    Yes, you may trust it. All ways of interpolation a variable are covered in the documentation pretty well.



    If you want to have a reason why this was done so, well, I can't help you there. But as always: PHP is old and has evolved a lot, thus introducing inconsistent syntax.






    share|improve this answer


























    • @nikic, I can't find this exact case (w/o curly braces) in the docs, where is it? Thanks, rbo

      – rubber boots
      Jan 19 '11 at 18:11













    • @mario: Personally I think it's not good, but many probably others are okay with it -> Dropped that part.

      – NikiC
      Jan 19 '11 at 18:12











    • @rubber boots: look out for this line: echo "He drank some $juices[koolaid1] juice.".PHP_EOL;.

      – NikiC
      Jan 19 '11 at 18:13






    • 2





      @mario, I come from Perl (still live there) and interpolation in Perl usually makes things more readable. The DMF in PHP here (dearly mising feature) is Perls q-operator (quote) series -> qq{}, q{}, qx{}, qw{}. Therefore (imho) string interpolation might be not as useful in PHP as it could be. :-(

      – rubber boots
      Jan 19 '11 at 18:20






    • 1





      @nikic somehow serve, right. But not on a single line. compare Perl: $x = qq{<a href="$link">$text</a>};

      – rubber boots
      Jan 19 '11 at 18:40


















    45














    Yes, you may trust it. All ways of interpolation a variable are covered in the documentation pretty well.



    If you want to have a reason why this was done so, well, I can't help you there. But as always: PHP is old and has evolved a lot, thus introducing inconsistent syntax.






    share|improve this answer


























    • @nikic, I can't find this exact case (w/o curly braces) in the docs, where is it? Thanks, rbo

      – rubber boots
      Jan 19 '11 at 18:11













    • @mario: Personally I think it's not good, but many probably others are okay with it -> Dropped that part.

      – NikiC
      Jan 19 '11 at 18:12











    • @rubber boots: look out for this line: echo "He drank some $juices[koolaid1] juice.".PHP_EOL;.

      – NikiC
      Jan 19 '11 at 18:13






    • 2





      @mario, I come from Perl (still live there) and interpolation in Perl usually makes things more readable. The DMF in PHP here (dearly mising feature) is Perls q-operator (quote) series -> qq{}, q{}, qx{}, qw{}. Therefore (imho) string interpolation might be not as useful in PHP as it could be. :-(

      – rubber boots
      Jan 19 '11 at 18:20






    • 1





      @nikic somehow serve, right. But not on a single line. compare Perl: $x = qq{<a href="$link">$text</a>};

      – rubber boots
      Jan 19 '11 at 18:40
















    45












    45








    45







    Yes, you may trust it. All ways of interpolation a variable are covered in the documentation pretty well.



    If you want to have a reason why this was done so, well, I can't help you there. But as always: PHP is old and has evolved a lot, thus introducing inconsistent syntax.






    share|improve this answer















    Yes, you may trust it. All ways of interpolation a variable are covered in the documentation pretty well.



    If you want to have a reason why this was done so, well, I can't help you there. But as always: PHP is old and has evolved a lot, thus introducing inconsistent syntax.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Jan 19 '11 at 18:08

























    answered Jan 19 '11 at 18:03









    NikiCNikiC

    82.2k27166211




    82.2k27166211













    • @nikic, I can't find this exact case (w/o curly braces) in the docs, where is it? Thanks, rbo

      – rubber boots
      Jan 19 '11 at 18:11













    • @mario: Personally I think it's not good, but many probably others are okay with it -> Dropped that part.

      – NikiC
      Jan 19 '11 at 18:12











    • @rubber boots: look out for this line: echo "He drank some $juices[koolaid1] juice.".PHP_EOL;.

      – NikiC
      Jan 19 '11 at 18:13






    • 2





      @mario, I come from Perl (still live there) and interpolation in Perl usually makes things more readable. The DMF in PHP here (dearly mising feature) is Perls q-operator (quote) series -> qq{}, q{}, qx{}, qw{}. Therefore (imho) string interpolation might be not as useful in PHP as it could be. :-(

      – rubber boots
      Jan 19 '11 at 18:20






    • 1





      @nikic somehow serve, right. But not on a single line. compare Perl: $x = qq{<a href="$link">$text</a>};

      – rubber boots
      Jan 19 '11 at 18:40





















    • @nikic, I can't find this exact case (w/o curly braces) in the docs, where is it? Thanks, rbo

      – rubber boots
      Jan 19 '11 at 18:11













    • @mario: Personally I think it's not good, but many probably others are okay with it -> Dropped that part.

      – NikiC
      Jan 19 '11 at 18:12











    • @rubber boots: look out for this line: echo "He drank some $juices[koolaid1] juice.".PHP_EOL;.

      – NikiC
      Jan 19 '11 at 18:13






    • 2





      @mario, I come from Perl (still live there) and interpolation in Perl usually makes things more readable. The DMF in PHP here (dearly mising feature) is Perls q-operator (quote) series -> qq{}, q{}, qx{}, qw{}. Therefore (imho) string interpolation might be not as useful in PHP as it could be. :-(

      – rubber boots
      Jan 19 '11 at 18:20






    • 1





      @nikic somehow serve, right. But not on a single line. compare Perl: $x = qq{<a href="$link">$text</a>};

      – rubber boots
      Jan 19 '11 at 18:40



















    @nikic, I can't find this exact case (w/o curly braces) in the docs, where is it? Thanks, rbo

    – rubber boots
    Jan 19 '11 at 18:11







    @nikic, I can't find this exact case (w/o curly braces) in the docs, where is it? Thanks, rbo

    – rubber boots
    Jan 19 '11 at 18:11















    @mario: Personally I think it's not good, but many probably others are okay with it -> Dropped that part.

    – NikiC
    Jan 19 '11 at 18:12





    @mario: Personally I think it's not good, but many probably others are okay with it -> Dropped that part.

    – NikiC
    Jan 19 '11 at 18:12













    @rubber boots: look out for this line: echo "He drank some $juices[koolaid1] juice.".PHP_EOL;.

    – NikiC
    Jan 19 '11 at 18:13





    @rubber boots: look out for this line: echo "He drank some $juices[koolaid1] juice.".PHP_EOL;.

    – NikiC
    Jan 19 '11 at 18:13




    2




    2





    @mario, I come from Perl (still live there) and interpolation in Perl usually makes things more readable. The DMF in PHP here (dearly mising feature) is Perls q-operator (quote) series -> qq{}, q{}, qx{}, qw{}. Therefore (imho) string interpolation might be not as useful in PHP as it could be. :-(

    – rubber boots
    Jan 19 '11 at 18:20





    @mario, I come from Perl (still live there) and interpolation in Perl usually makes things more readable. The DMF in PHP here (dearly mising feature) is Perls q-operator (quote) series -> qq{}, q{}, qx{}, qw{}. Therefore (imho) string interpolation might be not as useful in PHP as it could be. :-(

    – rubber boots
    Jan 19 '11 at 18:20




    1




    1





    @nikic somehow serve, right. But not on a single line. compare Perl: $x = qq{<a href="$link">$text</a>};

    – rubber boots
    Jan 19 '11 at 18:40







    @nikic somehow serve, right. But not on a single line. compare Perl: $x = qq{<a href="$link">$text</a>};

    – rubber boots
    Jan 19 '11 at 18:40















    10














    Yes, this is well defined behavior, and will always look for the string key 'key', and not the value of the (potentially undefined) constant key.



    For example, consider the following code:



    $arr = array('key' => 'val');
    define('key', 'defined constant');
    echo "$arr[key] within string is: $arr[key]";


    This will output the following:



    $arr[key] within string is: val


    That said, it's probably not best practice to write code like this, and instead either use:



    $string = "foo {$arr['key']}"


    or



    $string = 'foo ' . $arr['key']


    syntax.






    share|improve this answer






























      10














      Yes, this is well defined behavior, and will always look for the string key 'key', and not the value of the (potentially undefined) constant key.



      For example, consider the following code:



      $arr = array('key' => 'val');
      define('key', 'defined constant');
      echo "$arr[key] within string is: $arr[key]";


      This will output the following:



      $arr[key] within string is: val


      That said, it's probably not best practice to write code like this, and instead either use:



      $string = "foo {$arr['key']}"


      or



      $string = 'foo ' . $arr['key']


      syntax.






      share|improve this answer




























        10












        10








        10







        Yes, this is well defined behavior, and will always look for the string key 'key', and not the value of the (potentially undefined) constant key.



        For example, consider the following code:



        $arr = array('key' => 'val');
        define('key', 'defined constant');
        echo "$arr[key] within string is: $arr[key]";


        This will output the following:



        $arr[key] within string is: val


        That said, it's probably not best practice to write code like this, and instead either use:



        $string = "foo {$arr['key']}"


        or



        $string = 'foo ' . $arr['key']


        syntax.






        share|improve this answer















        Yes, this is well defined behavior, and will always look for the string key 'key', and not the value of the (potentially undefined) constant key.



        For example, consider the following code:



        $arr = array('key' => 'val');
        define('key', 'defined constant');
        echo "$arr[key] within string is: $arr[key]";


        This will output the following:



        $arr[key] within string is: val


        That said, it's probably not best practice to write code like this, and instead either use:



        $string = "foo {$arr['key']}"


        or



        $string = 'foo ' . $arr['key']


        syntax.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Jan 19 '11 at 18:24

























        answered Jan 19 '11 at 18:16









        mfondamfonda

        6,52712029




        6,52712029























            9














            The last one is a special case handled by the PHP tokenizer. It does not look up if any constant by that name was defined, it always assumes a string literal for compatibility with PHP3 and PHP4.






            share|improve this answer



















            • 5





              Just for those interested (probably nobody...): PHP will generate a T_STRING for the array index (or a T_NUM_STRING if it is a non-overflowing decimal number) instead of the normal T_CONSTANT_ENCAPSED_STRING.

              – NikiC
              Jan 19 '11 at 18:18
















            9














            The last one is a special case handled by the PHP tokenizer. It does not look up if any constant by that name was defined, it always assumes a string literal for compatibility with PHP3 and PHP4.






            share|improve this answer



















            • 5





              Just for those interested (probably nobody...): PHP will generate a T_STRING for the array index (or a T_NUM_STRING if it is a non-overflowing decimal number) instead of the normal T_CONSTANT_ENCAPSED_STRING.

              – NikiC
              Jan 19 '11 at 18:18














            9












            9








            9







            The last one is a special case handled by the PHP tokenizer. It does not look up if any constant by that name was defined, it always assumes a string literal for compatibility with PHP3 and PHP4.






            share|improve this answer













            The last one is a special case handled by the PHP tokenizer. It does not look up if any constant by that name was defined, it always assumes a string literal for compatibility with PHP3 and PHP4.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jan 19 '11 at 18:05









            mariomario

            124k17183254




            124k17183254








            • 5





              Just for those interested (probably nobody...): PHP will generate a T_STRING for the array index (or a T_NUM_STRING if it is a non-overflowing decimal number) instead of the normal T_CONSTANT_ENCAPSED_STRING.

              – NikiC
              Jan 19 '11 at 18:18














            • 5





              Just for those interested (probably nobody...): PHP will generate a T_STRING for the array index (or a T_NUM_STRING if it is a non-overflowing decimal number) instead of the normal T_CONSTANT_ENCAPSED_STRING.

              – NikiC
              Jan 19 '11 at 18:18








            5




            5





            Just for those interested (probably nobody...): PHP will generate a T_STRING for the array index (or a T_NUM_STRING if it is a non-overflowing decimal number) instead of the normal T_CONSTANT_ENCAPSED_STRING.

            – NikiC
            Jan 19 '11 at 18:18





            Just for those interested (probably nobody...): PHP will generate a T_STRING for the array index (or a T_NUM_STRING if it is a non-overflowing decimal number) instead of the normal T_CONSTANT_ENCAPSED_STRING.

            – NikiC
            Jan 19 '11 at 18:18











            0














            To answer your question, yes, yes it can, and much like implode and explode, php is very very forgiving... so inconsistency abound



            And I have to say I like PHP's interpolation for basical daisy punching variables into strings then and there,



            However if your doing only string variable interpolation using a single array's objects, it may be easier to write a template which you can daisy print a specific object variables into (like in say javascript or python) and hence explicit control over the variable scope and object being applied to the string



            I though this guy's isprintf really useful for this kind of thing



            http://www.frenck.nl/2013/06/string-interpolation-in-php.html



            <?php

            $values = array(
            'who' => 'me honey and me',
            'where' => 'Underneath the mango tree',
            'what' => 'moon',
            );

            echo isprintf('%(where)s, %(who)s can watch for the %(what)s', $values);

            // Outputs: Underneath the mango tree, me honey and me can watch for the moon





            share|improve this answer



















            • 1





              Link reports a 404 error

              – fpierrat
              Sep 29 '15 at 10:48
















            0














            To answer your question, yes, yes it can, and much like implode and explode, php is very very forgiving... so inconsistency abound



            And I have to say I like PHP's interpolation for basical daisy punching variables into strings then and there,



            However if your doing only string variable interpolation using a single array's objects, it may be easier to write a template which you can daisy print a specific object variables into (like in say javascript or python) and hence explicit control over the variable scope and object being applied to the string



            I though this guy's isprintf really useful for this kind of thing



            http://www.frenck.nl/2013/06/string-interpolation-in-php.html



            <?php

            $values = array(
            'who' => 'me honey and me',
            'where' => 'Underneath the mango tree',
            'what' => 'moon',
            );

            echo isprintf('%(where)s, %(who)s can watch for the %(what)s', $values);

            // Outputs: Underneath the mango tree, me honey and me can watch for the moon





            share|improve this answer



















            • 1





              Link reports a 404 error

              – fpierrat
              Sep 29 '15 at 10:48














            0












            0








            0







            To answer your question, yes, yes it can, and much like implode and explode, php is very very forgiving... so inconsistency abound



            And I have to say I like PHP's interpolation for basical daisy punching variables into strings then and there,



            However if your doing only string variable interpolation using a single array's objects, it may be easier to write a template which you can daisy print a specific object variables into (like in say javascript or python) and hence explicit control over the variable scope and object being applied to the string



            I though this guy's isprintf really useful for this kind of thing



            http://www.frenck.nl/2013/06/string-interpolation-in-php.html



            <?php

            $values = array(
            'who' => 'me honey and me',
            'where' => 'Underneath the mango tree',
            'what' => 'moon',
            );

            echo isprintf('%(where)s, %(who)s can watch for the %(what)s', $values);

            // Outputs: Underneath the mango tree, me honey and me can watch for the moon





            share|improve this answer













            To answer your question, yes, yes it can, and much like implode and explode, php is very very forgiving... so inconsistency abound



            And I have to say I like PHP's interpolation for basical daisy punching variables into strings then and there,



            However if your doing only string variable interpolation using a single array's objects, it may be easier to write a template which you can daisy print a specific object variables into (like in say javascript or python) and hence explicit control over the variable scope and object being applied to the string



            I though this guy's isprintf really useful for this kind of thing



            http://www.frenck.nl/2013/06/string-interpolation-in-php.html



            <?php

            $values = array(
            'who' => 'me honey and me',
            'where' => 'Underneath the mango tree',
            'what' => 'moon',
            );

            echo isprintf('%(where)s, %(who)s can watch for the %(what)s', $values);

            // Outputs: Underneath the mango tree, me honey and me can watch for the moon






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered May 6 '14 at 9:10









            aqmaqm

            2,1061425




            2,1061425








            • 1





              Link reports a 404 error

              – fpierrat
              Sep 29 '15 at 10:48














            • 1





              Link reports a 404 error

              – fpierrat
              Sep 29 '15 at 10:48








            1




            1





            Link reports a 404 error

            – fpierrat
            Sep 29 '15 at 10:48





            Link reports a 404 error

            – fpierrat
            Sep 29 '15 at 10:48


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f4738850%2finterpolation-double-quoted-string-of-associative-arrays-in-php%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Homophylophilia

            Updating UILabel text programmatically using a function

            Cloud Functions - OpenCV Videocapture Read method fails for larger files from cloud storage