Solved

function in hereDocs

Posted on 2016-11-07
1
22 Views
Last Modified: 2016-11-08
Is it possible to call a function from inside a heredoc?

For example, if I wanted to call date('Y-m-d', $rwTrans['dateTime']) inside the heredoc:-
    echo <<<EOD
      <tr data-transID="{$rwTrans['transID']}">
        <td>{date('Y-m-d', $rwTrans['dateTime'])}</td>
        <td>$transNote</td>
        <td>{$rwTrans['amount']}</td>
        <td>{$rwTrans['status']}</td>
      </tr>
EOD;

Open in new window


Any ideas?
0
Comment
Question by:tonelm54
1 Comment
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 41877985
It doesn't work that way.  You get variable substitution in HEREDOC, but not function calls.  So call the function outside of the HEREDOC, set a variable, and use the variable in the HEREDOC block.  Something like this...
    // THIS ASSUMES THAT $rwTrans['dateTime'] CONTAINS A UNIX TIMESTAMP
    $datexxx = date('Y-m-d', $rwTrans['dateTime']);
    
    echo <<<EOD
      <tr data-transID="{$rwTrans['transID']}">
        <td>$datexxx</td>
        <td>$transNote</td>
        <td>{$rwTrans['amount']}</td>
        <td>{$rwTrans['status']}</td>
      </tr>
EOD;

Open in new window

0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question