57

I use PHP's EOF string to format HTML content without the hassle of having to escape quotes etc. How can I use the function inside this string?

<?php
    $str = <<<EOF
    <p>Hello</p>
    <p><?= _("World"); ?></p>
EOF;
    echo $str;
?>
A.L
  • 10,259
  • 10
  • 67
  • 98
FFish
  • 10,964
  • 34
  • 95
  • 136

2 Answers2

104

As far as I can see in the manual, it is not possible to call functions inside HEREDOC strings. A cumbersome way would be to prepare the words beforehand:

<?php

    $world = _("World");

    $str = <<<EOF
    <p>Hello</p>
    <p>$world</p>
EOF;
    echo $str;
?>

a workaround idea that comes to mind is building a class with a magic getter method.

You would declare a class like this:

class Translator
{
 public function __get($name) {
  return _($name); // Does the gettext lookup
  }
 }

Initialize an object of the class at some point:

  $translate = new Translator();

You can then use the following syntax to do a gettext lookup inside a HEREDOC block:

    $str = <<<EOF
    <p>Hello</p>
    <p>{$translate->World}</p>
EOF;
    echo $str;
?>

$translate->World will automatically be translated to the gettext lookup thanks to the magic getter method.

To use this method for words with spaces or special characters (e.g. a gettext entry named Hello World!!!!!!, you will have to use the following notation:

 $translate->{"Hello World!!!!!!"}

This is all untested but should work.

Update: As @mario found out, it is possible to call functions from HEREDOC strings after all. I think using getters like this is a sleek solution, but using a direct function call may be easier. See the comments on how to do this.

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • 1
    @FFish you're welcome. It's untested so I can't give you a total guarantee but as far as I can see, this will work. See my latest update (regarding spaces and special characters) for how to deal with more complex gettext identifiers – Pekka Sep 12 '10 at 09:37
  • 8
    Turns out you can use function calls. With `$_="_";` and `{$_('text')}`. But the getter syntax is +1. – mario Sep 12 '10 at 09:53
  • @mario oh wow, didn't know that! That would be worth a separate answer IMO. – Pekka Sep 12 '10 at 09:55
  • 3
    Thanks for the reply Mario. Here is what was missing: $_ = "gettext"; // assigns function reference for HEREDOC use. Than {$_("World")} works inside the HEREDOC string! – FFish Sep 14 '10 at 13:25
  • @Pekka Can I use `foreach` iside `<< – MAHA OUEGHLANI Jun 05 '23 at 10:08
-3

As far as I can see, you just added heredoc by mistake
No need to use ugly heredoc syntax here.
Just remove it and everything will work:

<p>Hello</p>
<p><?= _("World"); ?></p>
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • 6
    I don't think "Hello World" is an excerpt from the OP's actual production code. – Pekka Sep 12 '10 at 13:24
  • @Pekka it doesn't matter. Heredoc is still useless no matter of size – Your Common Sense Sep 12 '10 at 13:29
  • 3
    To me, there is a number of situations where using Heredoc is a fine alternative to using a template engine of some sort. The only big downside to it in my opinion is that the end marker can't be indented, which tends to screw up indented code classes – Pekka Sep 12 '10 at 13:31
  • What are your arguments against Heredoc apart from that it is ugly? I can't see any devastating downside to it, but I'm always open to learn – Pekka Sep 12 '10 at 13:32
  • @Pekka look at the code. it has PHP tags in it. I think heredoc were added by accident. And while heredoc can be an *alternative*, plain and native HTML is always preferred. isn't it obvious? – Your Common Sense Sep 12 '10 at 13:38
  • 1
    @Col I totally agree, but the code the OP presented is merely an *example* to explain the question, isn't it? – Pekka Sep 12 '10 at 13:41
  • @Col You know I mostly agree with you on the issue of [bad practice answers](http://meta.stackexchange.com/questions/64227/bad-practice-answers-why-closed) (mostly, not entirely, because I think people are entitled to know how to use Na and Cl separately) but in this specific question, there is no indication of what the OP is actually using Heredoc for. So if you are saying "Heredoc is *always* bad", I'm open to learn if you have good arguments. If you're not saying that, I don't see how your answer has any value to the question. – Pekka Sep 12 '10 at 13:44
  • Especially since the OP explicitly says he wants to use Heredoc: `I use PHP's EOF string to format HTML content without the hassle of having to escape quotes etc` ... but it's no big deal either way, I'm just splitting hairs on a sunday afternoon :) – Pekka Sep 12 '10 at 13:48
  • @Pekka i see no point in your argues. Downvote my answer and leave me alone. Unlike you I am reading not only *statement* `use PHP's EOF string` but also *reasoning* `without the hassle of having to escape quotes`. And Plain HTML is even better because it lets you format HTML content without the hassle of heredoc either. Large blocks of HTML are always better as HTML, not PHP code. If you don't see it its up to you. – Your Common Sense Sep 12 '10 at 13:51
  • @Col I do see that and agree, but it's not the OP's question. Anyway - it doesn't matter either way. – Pekka Sep 12 '10 at 13:55
  • 3
    Years late to the party, but I'll point out a situation where heredoc cannot be simply removed: when you want to assign the markup to a variable and do something else with it, rather than immediately outputting it. That's my situation, why I ended up here. – OsakaWebbie May 03 '19 at 06:55
  • In this "answer" you are giving your opinion about heredoc, not answering the question. And your "arguments" about why heredoc is "useless" are actually useless. – El Gucs Apr 20 '23 at 19:17