Learn how to a build a cloud-first strategyRegister Now


how to call a function within a heredoc string

Posted on 2010-01-04
Medium Priority
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

Assisted Solution

marklogan earned 500 total points
ID: 26169929
"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 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 500 total points
ID: 26171235
"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

Accepted Solution

pg-expert earned 0 total points
ID: 26178564
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses
Course of the Month21 days, 7 hours left to enroll

810 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