PHP using Gettext inside <<<EOF string

PHP using Gettext inside <<<EOF string

  1. Blog
  2. 15 November 2026
  3. 1 min read

Stefan Bogdanescu

Founder & Senior Architect

Why gettext Doesn't Work Inside <<<EOF Strings and How to Fix It

You often see developers using PHP's heredoc syntax (<<<EOF) to build large blocks of HTML or XML without wrestling with quote escaping. It keeps the code clean and readable. However, a common stumbling block arises when trying to inject localized strings directly into that block using gettext() or its translation function _().

Consider this scenario: you want to generate a paragraph containing "Hello" and another containing a translated "World". A naive attempt might look like this:

$str = <<<EOF
    <p>Hello</p>
    <p><?= _("World"); ?></p>
EOF;
echo $str;

This code fails to output the intended translation because of how PHP's parser handles heredoc strings versus function calls. In a heredoc, everything between the opening delimiter and the closing EOF is treated as a literal string until it hits the end. The parser does not execute variable interpolations or function calls found within that block in the way you might expect from standard double-quoted strings unless specific syntax rules are followed.

Specifically, the <?= short tag inside a heredoc creates a parsing conflict. When PHP encounters the heredoc start, it enters a "literal string" mode. While variables like $name work automatically because they are interpolated into the string representation, function calls like _("World") do not execute at runtime within that block in the same context. The parser sees _("World") as literal characters to be printed, not code to run.

To make this work, you must leverage PHP's variable interpolation feature. You need to wrap the translated content in curly braces {} or simply rely on the fact that heredocs support variable interpolation but treat function calls differently depending on their placement relative to the string context. However, the most reliable pattern for mixing static text with dynamic translations inside a heredoc is to separate the static HTML structure from the dynamic content entirely.

Instead of trying to force _("World") to execute inside the heredoc block using short tags (which often leads to unexpected parsing behavior depending on the PHP version and configuration), you should interpolate the result of the translation function into the string before the heredoc closes, or use a double-quoted string for that specific segment.

Here is a robust approach that maintains readability:

// Define the static HTML structure
$template = <<<HTML
    <p>Hello</p>
    <p>{content}</p>
HTML;

// Resolve the translation outside the heredoc context
$translation = _("World");

// Interpolate the value into the template
$str = str_replace('{content}', $translation, $template);
echo $str;

This method ensures that _("World") is executed by the PHP engine before the string is finalized. It avoids the ambiguity of mixing short open tags inside heredocs and guarantees that the translation logic runs correctly.

If you are working within a Laravel ecosystem, the $translator facade or the helper _() function behaves consistently with this interpolation requirement. You might also consider using sprintf if your translations involve placeholders:

$t = _("Welcome %s", 'User');
$str = sprintf(
    <<<HTML
    <p>Welcome</p>
    <p>{msg}</p>
    HTML
, $t);
$str = str_replace('{msg}', $t, $str);

While this adds a line or two of code, it removes the risk of syntax errors and ensures your internationalization strategy remains decoupled from your template rendering logic. This separation of concerns is often cleaner than trying to embed complex function calls directly inside heredoc blocks.

Our network