Skip to content

PHP using Gettext inside <<<EOF string

It is possible to use the gettext function inside a heredoc string in PHP. A heredoc string is a string that is defined using the <<<EOF syntax.

Here is an example of how you can use gettext inside a heredoc string:

Example of using Gettext inside <<<EOF string in PHP

php
<?php

$name = "John";

echo <<<EOF
Hello $name!

Welcome to the website.

EOF;

<div class="alert alert-info flex not-prose"> Watch a course Learn object oriented PHP</div>

To use gettext inside a heredoc string, you can simply call the gettext function like you would anywhere else in your code.

For example:

Example of using gettext function inside a heredoc string in PHP

php
<?php

// Define a variable
$name = 'John';

// Use gettext inside a heredoc string
echo <<<EOF
<p>Hello, {$name}! How are you?</p>
<p>This is an English translation.</p>
EOF;

Do you find this helpful?

Dual-run preview — compare with live Symfony routes.