function call in double quote doesn't work

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • datactrl
    New Member
    • Jul 2008
    • 51

    function call in double quote doesn't work

    With PHP 5, why doesn't it work as following. It always shows "--->" instead of "--->zzz".

    [CODE=php]
    $aa = "--->{${getName()}} ";
    echo $aa;

    function getName(){
    return 'zzz';
    }
    [/CODE]

    Jack
    Last edited by Atli; Oct 17 '08, 01:55 AM. Reason: Added [code] tags.
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Jack.

    Please use [code] tags when posting your code examples.

    [code=ph p] ... PHP code goes here ... [/code]

    Thank you.
    Moderator

    Comment

    • Atli
      Recognized Expert Expert
      • Nov 2006
      • 5062

      #3
      You can only use variables, array elements and object properties inside double quotes.

      But... you can use the return value of a function or a method to dynamically get a variable name.

      Consider this:
      [code=php]
      function getVarName() {
      return 'zzz';
      }
      $zzz = 'Var value';
      echo "Value of variable = {${getVarName() }}";

      // Prints: Value of variable = Var value
      [/code]
      See Variable variables to see how that works.

      Comment

      • datactrl
        New Member
        • Jul 2008
        • 51

        #4
        Thank you so much, Moderator. I will remember to code php code before post. Now I understand how it works on functions return value in double quote. Why do I ask this is because I want to insert some values returned from functions in here-document. Now, only way to do is setting variables from functions then include those variables in here-document.
        Jack

        Comment

        Working...