Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Echo a large block of text

Tags:

html

php

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)?

like image 935
Thomas Avatar asked Apr 20 '10 04:04

Thomas


People also ask

Can you echo PHP code?

You cannot have PHP echo more PHP code to be evaluated because PHP interprets your code in a single pass.

What does PHP echo do?

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.

What is the output of the PHP script <? PHP echo I am a PHP script ?>?

Answer. It output one or more strings. We can use 'echo' to output strings, numbers, variables, values, and results of expressions.


2 Answers

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; 
like image 192
nickf Avatar answered Sep 23 '22 06:09

nickf


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.

like image 34
hookedonwinter Avatar answered Sep 23 '22 06:09

hookedonwinter