Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1057859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:56:13+00:00 2026-05-16T17:56:13+00:00

I use PHP’s EOF string to format HTML content without the hassle of having

  • 0

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;
?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T17:56:14+00:00Added an answer on May 16, 2026 at 5:56 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I can't use PHP in my HTML pages. For example, index.html . I've tried
I want to use PHP to replace javascript functions in HTML documents. For example:
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I use PHP code below for login without using database. It redirects to home.php
I use PHP to generate files of some special format, and I have decided
I use Php memcache on PHP Version 5.2.4-2ubuntu5.10 Below you can find the info
Is it possible to use php's preg_replace to remove anything in a string except
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
How can I use PHP to trim all white space up to poo and
How can I use PHP to move a file? Code: if( file_exists($imageLocation) ) echo

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.