how to call a function within a heredoc string

Posted on 2010-01-04
Last Modified: 2013-12-12
is it possible to directly call a function within a heredoc string.

i know we can use a local variable to store the function result and then use it in heredoc

is there a better way to do or any alternatives?
Question by:pg-expert
    LVL 7

    Assisted Solution

    "Note: Functions, method calls, static class variables, and class constants inside {$} work since PHP 5. However, the value accessed will be interpreted as the name of a variable in the scope in which the string is defined. Using single curly braces ({}) will not work for accessing the return values of functions or methods or the values of class constants or static class variables. "

    You are best doing it the way you currently are.

    function getName(){
    	return "Timmy";
    $Timmy = "Bob";
    echo "My Name is: {${getName()}}";
    $beer = 'Heineken';
    echo "He drank some {$beer}s";

    Open in new window

    LVL 107

    Assisted Solution

    by:Ray Paseur
    "use a local variable to store the function result and then use it in heredoc"

    Yes, do it that way.  The more levels of "compound code" you create, the more certain you can be that disaster is not left to chance.  I am a BIG FAN of writing the simplest, clearest code (you already knew that, right?)

    Best, ~Ray
    LVL 9

    Accepted Solution

    Thanks experts for ur comments,

    The best way is to use a local variable to store the function result and then use it in heredoc.

    i dont see any other best way.


    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    A colleague recently asked me about how to give his client a small part of the web site that could be completely under the client's control.  Since I have done this sort of thing before to add emergency banners to a web site, I decided I would creat…
    Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

    745 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

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now