Solved

function in hereDocs

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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…
The viewer will learn how to count occurrences of each item in an array.

895 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

13 Experts available now in Live!

Get 1:1 Help Now