Solved

function in hereDocs

Posted on 2016-11-07
1
15 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 108

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article discusses four methods for overlaying images in a container on a web page
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

746 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now