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"/><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)?
You cannot have PHP echo more PHP code to be evaluated because PHP interprets your code in a single pass.
The echo is used to display the output of parameters that are passed to it. It displays the outputs of one or more strings separated by commas. The print accepts one argument at a time & cannot be used as a variable function in PHP.
Answer. It output one or more strings. We can use 'echo' to output strings, numbers, variables, values, and results of expressions.
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;
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With