/    Sign up×
Community /Pin to ProfileBookmark

Calculate Within <<<EOT ?

Hello all.

I’m using echo <<<EOT to simplify my effort to write html and js file output.

Sometimes, I need to calculate something in the midst of it and I’m wondering if there’s a way to perform such a calculation on the spot instead of having to do …

EOT;
echo (some calculation);
echo <<<EOT

Know what I mean?

So, let’s say (intending to be a .js file output) …

$something = 25;
echo <<<EOT
var jojo = $something * 4;
alert(jojo);
EOT;

… is there a way to pre-calculate “$something * 4” without having to end the <<<EOT output? Something short and sweet?

If not, no biggy.

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@NogDogFeb 10.2009 — No, but you could do it [i]before[/i] the heredoc string:
[code=php]
<?php
$result = $foo * $bar;
echo <<<EOT
Hello, World.
The product of $foo * $bar is $result.
The end.
EOT;

?>
[/code]

Or you could use [s]printf():

[code=php]
<?php
$html = <<<EOT
Hello, World.
The product of &#37;d * %d is %d.
The end.
EOT;

printf($html, $foo, $bar, $foo * $bar);
?>
[/code]
Copy linkTweet thisAlerts:
@robindeanauthorFeb 10.2009 — Good plan. Thanks!
×

Success!

Help @robindean spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.1,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...