3

In PHP I can say

$noun = "bird";
$adjective = "warm;
echo <<<EOT
The $noun is very $adjective
EOT;

and it will output

The bird is very warm

Is there a way to do this with functions?

function getNoun() { return "bird"; }
function getAdjective() { return "warm"; }
echo <<<EOT
The getNoun() is very getAdjective()
EOT;

and output

The bird is very warm

3
  • Just curious, for what do you need this? Commented Dec 2, 2011 at 21:42
  • I am building a set of blogging API and I want to have a makeLink(anchor, target, format) function and put it inside of a herdoc. It has to be a heredoc string because it has multiple quotes and apostrophes. I would like it if i could embed a function in a heredoc, not just for links, but tables, pictures, posts, I think its cleaner and more readable to use an aptly named function that writes out the HTML for you than just ambiguous HTML. makePost("About HTML", "By ME", "Lorem Ipsum...,"); is so much more meaningful then <div id="post"><div id="title"><h1>AboutHTML</h1><h2>By ME</h2></div ... Commented Dec 13, 2011 at 4:55
  • Please show the code what you've done so far, probably we can help with a concrete problem of your parser. Commented Dec 13, 2011 at 8:03

4 Answers 4

5

You can use variable functions, though they're rather frowned on upon as they're not far different from variable variables...

$a = 'getNoun';
$b = 'getAdjective';
echo <<<EOT
The {$a()} is very {$b()}
EOT;
Sign up to request clarification or add additional context in comments.

Comments

2

You could store it in a variable before using it:

$noun = getNoun( );
$adj = getAdjective( );
echo <<<EOT
    The {$noun} is very {$adj}
EOT;

Comments

0
echo "The ";
echo getNoun();
echo " is very ";
echo getVerb();

Or maybe even (not 100% sure):

echo "The " . getNoun() . " is very " . getVerb();

Comments

0

Here is a solution using variable functions, although not exactly:

$noun=function($type){
    return $type;
};
$adjective=function($adj){
    return $adj;
};
echo "{$noun("Robin")} was very {$adjective("Warm")}";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.