/    Sign up×
Community /Pin to ProfileBookmark

Tidily embedding JS into PHP

Hi all,

A simple way to embed JS into PHP is the following:
<?php
$a = 5;
echo “<html>n”;
echo “<body>n”;
echo “<script language=javascript>n”;
echo “document.write($a);n”;
echo “</script>n”;
echo “</body>n”;
echo “</html>n”;
?>

I’m looking for a ‘tidier’ way to do this. For instance, a way in which command ‘echo’ is not repeated so many times…

Comments welcome.

Ruben

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@TJ111Jan 22.2008 — Use HEREDOC syntax.
[code=php]
$a = 5;
echo <<<EndHTML;
<html>
<body>
<script type="text/javascript">
document.write($a);
</script>
</body>
</html>
EndHTML;
[/code]

You can use whatever word you want for "EndHTML", just make sure it doesn't appear in the string itself, and that it appears on its own line with no indentation.
Copy linkTweet thisAlerts:
@rvazquezauthorJan 22.2008 — Thanks for your answer ?

But, this use of command echo doesn't work in my server... have you got any idea on why?

Ruben
Copy linkTweet thisAlerts:
@TJ111Jan 22.2008 — You might need to store it in a variable, then echo the variable.
[code=php]
$a = 5;
$html = <<<EndHTML;
<html>
<body>
<script type="text/javascript">
document.write($a);
</script>
</body>
</html>
EndHTML;
echo $html;[/code]
Copy linkTweet thisAlerts:
@MrCoderJan 22.2008 — [code=php]
<?php
ob_start();
?>
// All my javascript here
document.write(<?php echo $a; ?>);
// End of my javascript
<?php
$javascript = ob_get_clean();
?>

<html>
<body>
<script type="text/javascript">
<?php echo $javascript; ?>
</script>
</body>
</html>

[/code]
Copy linkTweet thisAlerts:
@rvazquezauthorJan 23.2008 — Any idea on why this variable initialization works:

<?php

ob_start();

$a = 5;

?>

and this one doesn't work:

<?php

ob_start();

$a = "Hello world!";

?>

Ruben
Copy linkTweet thisAlerts:
@rvazquezauthorJan 23.2008 — In other words...

This works:

// All my javascript here

document.write(<?php echo 5; ?>);

// End of my javascript

This doesn't work:

// All my javascript here

document.write(<?php echo "Hello World"; ?>);

// End of my javascript

Ruben
Copy linkTweet thisAlerts:
@MrCoderJan 23.2008 — <i>
</i>This doesn't work:
// All my javascript here
document.write('&lt;?php echo "Hello World"; ?&gt;');
// End of my javascript
×

Success!

Help @rvazquez 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.21,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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