Solved

function in hereDocs

Posted on 2016-11-07
1
37 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 110

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This article discusses how to implement server side field validation and display customized error messages to the client.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

724 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