/    Sign up×
Community /Pin to ProfileBookmark

Hi,

I’m trying to write a bit of javascript using php, but I can’t get it to go onto a new line,

if I enter

echo ‘<script type=”text/javascript”> n’ ;
echo ‘$(document).ready(function() {‘;

when I view the page source both lines above appear on the same line, I’ve tried using single and double quotes but when I use double quotes the page doesn’t load at all,

Any help would be appreciated.

Cheers

Tim

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@aj_nscOct 05.2008 — I actually can't see why you're code doesn't work...but if it doesn't you could always skip the use of an echo statement all together:

[code=php]
<?php
//your php code here
?>
<script type="text/javascript">
<?php
//more php code here
?>
</script>
<?php
//more php code here
?>
[/code]
Copy linkTweet thisAlerts:
@sstalderOct 05.2008 — You have to write it like this:
[code=php]
echo "<script type="text/javascript"> n" ;
echo "$(document).ready(function() {";
[/code]
Copy linkTweet thisAlerts:
@ShortsOct 06.2008 — sstalder is correct.

You need to use Double Quotes if you want it to render elements like newline (n), tab(t), etc. Same this variables, you need Double Quotes. Single quotes only spit out whatever you put in it, meaning it does not format the string at all.

Also Steve, I like your site. Very clean.
Copy linkTweet thisAlerts:
@sstalderOct 06.2008 — Thank you ?
Copy linkTweet thisAlerts:
@ZnupiOct 06.2008 — Bah, the old dollar sign error. sstalder, you were close, but you missed one error: in double-quoted strings, the $ signs are parsed as if a variable name comes after them, so they need to be escaped. So the correct could would be:
[code=php]
echo "<script type="text/javascript"> n" ;
echo "$(document).ready(function() {";
[/code]
Copy linkTweet thisAlerts:
@sstalderOct 06.2008 — Good catch, wasn't even paying attention to that.
Copy linkTweet thisAlerts:
@ShortsOct 06.2008 — Actually I noticed the $ sign but it shouldn't be a problem there since it is followed by a '('.

Although personally I'd still go with the single quotes :]
[code=php]
echo '$(document).ready(function() {';
[/code]

Which also helps to give an example when to use single and when to use double quotes :]
×

Success!

Help @SylvesterWebste 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.18,
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,
)...