tags:

views:

557

answers:

6

Im new to PHP and I can't figure out what the rules are for using the echo function. For example, if I need to echo a large block of css/js, do I need to add echo to each line of text or is there a way to echo a large block of code with a single echo?

When I try to echo a big block of code like this one, I get an error:

if (is_single()) {
echo '<link type="text/css" rel="stylesheet" href="http://jotform.com/css/styles/form.css"/&gt;&lt;style type="text/css"> 
.form-label{
width:150px !important;
}
.form-label-left{
width:150px !important;
}
.form-line{
padding:10px;
}
.form-label-right{
width:150px !important;
}
body, html{
margin:0;
padding:0;
background:false;
}

.form-all{
margin:0px auto;
padding-top:20px;
width:650px !important;
color:Black;
font-family:Verdana;
font-size:12px;
}
</style> 

<link href="http://jotform.com/css/calendarview.css" rel="stylesheet" type="text/css" /> 
<script src="http://jotform.com/js/prototype.js" type="text/javascript"></script> 
<script src="http://jotform.com/js/protoplus.js" type="text/javascript"></script> 
<script src="http://jotform.com/js/protoplus-ui.js" type="text/javascript"></script> 
<script src="http://jotform.com/js/jotform.js?v3" type="text/javascript"></script> 
<script src="http://jotform.com/js/location.js" type="text/javascript"></script> 
<script src="http://jotform.com/js/calendarview.js" type="text/javascript"></script> 
<script type="text/javascript"> 

JotForm.init(function(){
$('input_6').hint('ex: [email protected]');
});
</script>';
}else {

}

Is there a better way to echo large blocks of code without a lot of work (adding echo to each line for example)?

A: 

check out heredoc

Galen
+9  A: 

One option is to get out of the php block and just write HTML.

With your code, after the opening curly brace of your if statement, end the PHP:

if (is_single()) { ?>

Then remove the echo ' and the ';

After all your html and css, before the closing }, write:

<? } else {

If the text you want to write to the page is dynamic, it gets a little trickier, but for now this should work fine.

hookedonwinter
+4  A: 

Heredoc syntax can be very useful:

// start the string with 3 <'s and then a word
// it doesn't have to be any particular string or length
// but it's common to make it in all caps.
echo <<< EOT
    in here is your string
    it has the same variable substitution rules
    as a double quoted string.
    when you end it, put the indicator word at the
    start of the line (no spaces before it)
    and put a semicolon after it
EOT;
nickf
I'm not a fan of heredoc syntax, but I give you a +1 because you're the first one to actually illustrate what heredoc syntax is here, making it a useful answer.
thomasrutter
@thomasrutter - I also am not a huge fan of it: many IDEs don't deal with it well, and it either stuffs up your code indentation or you get a string with a ton of useless whitespace.
nickf
+1  A: 

Echoing text that contains line breaks is fine, and there's no limit on the amount of text or lines you can echo at once (save for available memory).

The error in your code is caused by the unescaped single quotes which appear in the string.

See this line:

$('input_6').hint('ex: [email protected]');

You'd need to escape those single quotes in a PHP string whether it's a single line or not.

There is another good way to echo large strings, though, and that's to close the PHP block and open it again later:

if (is_single()) {
  ?>
<link type="text/css" rel="stylesheet" href="http://jotform.com/css/styles/form.css"/&gt;&lt;style type="text/css"> 
.form-label{
width:150px !important;
}
.form-label-left{
width:150px !important;
}
.form-line{
padding:10px;
}
.form-label-right{
width:150px !important;
}
body, html{
margin:0;
padding:0;
background:false;
}

.form-all{
margin:0px auto;
padding-top:20px;
width:650px !important;
color:Black;
font-family:Verdana;
font-size:12px;
}
</style> 

<link href="http://jotform.com/css/calendarview.css" rel="stylesheet" type="text/css" /> 
<script src="http://jotform.com/js/prototype.js" type="text/javascript"></script> 
<script src="http://jotform.com/js/protoplus.js" type="text/javascript"></script> 
<script src="http://jotform.com/js/protoplus-ui.js" type="text/javascript"></script> 
<script src="http://jotform.com/js/jotform.js?v3" type="text/javascript"></script> 
<script src="http://jotform.com/js/location.js" type="text/javascript"></script> 
<script src="http://jotform.com/js/calendarview.js" type="text/javascript"></script> 
<script type="text/javascript"> 

JotForm.init(function(){
$('input_6').hint('ex: [email protected]');
});
</script>
  <?php
}else {

}

Or another alternative, which is probably better for readability, is to put all that static HTML into another page and include() it.

thomasrutter
+1  A: 

Your problem is actually caused by:

$('input_6').hint('ex: [email protected]');

You need to escape the single quotes to be \'

However: Using a Heredoc is a much better idea, as it will be much cleaner overall.

webdestroya
uh oh care to prove about a heeredoc? and what about templates? And what about syntax highlight? Are you fan of editing huge HTML not as HTML but as single colored PHP string?
Col. Shrapnel
I prefer templates (Smarty), but in this case that would seem a bit too elaborate for his question. I am a fan of using Heredoc over a massive `echo` statement. Your solution is also good too, but it doesn't allow me to put variables into the text (easily) I would have to do `<?php echo $blah;?>` which can get annoying.
webdestroya
aha you prefer bad over worst. time to grow up, to good things :)
Col. Shrapnel
"which can get annoying" OMG, open your eyes! every current framework uses it for templates, instead of monstrous and useless smarty
Col. Shrapnel
I like doing `{$variable}` instead of `<?php echo $variable;?>` it's just a personal preference...
webdestroya
@webdestroya a middle ground, if it's turned on you can use php shorthand and do <?= $variable ?>
hookedonwinter
+2  A: 

Man, PHP is not perl!
PHP can just escape from HTML :) http://www.php.net/manual/en/language.basic-syntax.phpmode.php

if (is_single()) {
//now we just close PHP tag
?>
</style> 
<script> 
<blah blah blah>
<?php
//open it back. here is your PHP again. easy!
}
?>

I wonder why such many people stuck to ugly heredoc.

Col. Shrapnel
Heredoc isn't *that* ugly, and is useful if you want to create a string and/or interpolate a lot of variables. Otherwise, the PHP escape is a much cleaner solution. But I think I'm glad Perl can't do that.
Duncan
@Duncan time to learn templates :)
Col. Shrapnel
@Col. Shrapnel: Oh I have ... but the Perl code I have been maintaining is enough of a nightmare without it being mixed in with HTML. Especially since HTML would probably compile in Perl :)
Duncan