Using PHP with HTML code inside an Echo Statement

I am trying to write an echo statement to the page that puts a textarea on the page with a red color for the text.

Normal HTML code would like this and my text would be in red

<textarea style="color:#FF0000;" name="message">Please Enter</textarea>

Now I have tried this and I can’t get it to work (it is part of a bigger script I have just trimmed it down for this example.)

<?php
{echo '<textarea style=\\"color:#FF0000;\\" name=\\"message\\">Please Enter</textarea>';}
?>

Cheers


echo '<textarea style="color:#FF0000;" name="message">Please Enter</textarea>';

Read
http://www.php.net/types.string

Cheers that is perfect

If your HTML statement is echoed between single quotes, you do not need to use escape characters. This is only if you’re placing it between double quotes.

Or if you want to do it a better way, just don’t EVER echo html


<?
ob_start();
?>
<textarea style="color:#FF0000;" name="message">Please Enter</textarea>
<?
echo ob_get_clean();
?>

This method is great for printing a large amount of html without having to worry about the html quotes and stuff.

hmmm…what if you have variables that need to be output inside HTML?

What kind of syntax do you use?

curious…

zurpit - the heredoc syntax is a far more efficient way that using object buffering like that.


echo <<<EOD
A big "block of text", with <whatever> characters you want.
EOD;

Galen and Zurpit.com’s suggestions are a very good way of doing it. If you need to use a variable within HTML statement, you can just insert the <? echo “…” ?> between the HTML such as:


<? ob_start(); ?>
<textarea style="color:#FF0000;" name="message">
[B][COLOR="Blue"]<? echo $variable; ?>[/COLOR][/B]
</textarea>
<? echo ob_get_clean(); ?>

Or another example for setting the colour from a variable would be:


<? ob_start(); ?>
<textarea style="color[B][COLOR="blue"]:<? echo $colour; ?>[/COLOR][/B];" name="message">Message</textarea>
<? echo ob_get_clean(); ?>

Good point, another clean way of printing a php variable in your html:

instead of using:


<? echo $variable; ?>

you can use:


<?=$variable;?>

I need to get used to all these shortcuts in PHP. They’re very useful and will probably save a lot of time in the long-term.

Thanks for the tip. I’ll be using that from now on.

You can also do conditional statement:

instead of:


<?
if($isTrue){
    echo "true";
}else{
    echo "false";
}
?>

you can do:


<?=($isTrue)?"true":"false";?>

don’t echo html!

<?php if($isTrue): ?>
true
<?php else: ?>
false
<?php endif; ?>

echoing HTML is good sometimes.

You can do this:



<?php if($isValid): ?>

<span class="valid">Some Text</span>

<?php else: ?>

<span class="invalid">Some Text</span>

<?php endif; ?>


Or do this (much shorter)


<span class=<?=($isValid)?"invalid":"valid";?>>Some Text</span>

ick…I find it annoying to see opening and closing php tags every other line and would rather just include HTML in the echo.

In loops and conditionals especially…IMHO it looks messy to break in and out of php just to output HTML.

Is there that big of a performance difference?

I think by integrating the HTML in the echo, it makes the code easier to read. I would be interested to know if there is a performance difference between the two as well.

AFAIK, if there are lots of HTMLs in the echo ‘’ (again there might be slightly difference double quotes and single quotes too) then of course there will be performance difference since the PHP compiler needs to compile the whole HTMLs as a PHP code. So if there is a simple and short HTML to print in between a long PHP code then better to echo it otherwise just do as galen says above.

I’ve been finishing off a project for a client where the previous developer has put almost all of a pages HTML within ECHO’s. I’m find it really difficult to navigation my way around the code.

I like to keep as much HTML as possible as HTML, this way, I can see DIV’s and tables in dreamweaver, which makes amending layouts much easier. If you put a table or div with PHP code, it’s really difficult to make changes as you can’t visually see the DIV or tables when you’re in dreamweaver.

Yesterday, I had a thought. PHP on the server parses everything between the PHP tags, so, the more PHP there is, the more code needs to be parsed, and surely this will have an effect on speed. On a small project I doubt you’d see much difference, however on a larger project where there essentially could be thousands of lines of additional code caused by HTML in ECHO’s, it’s going to have a bigger effect.

I wanted to test this for myself, and found a little script which starts a timer, and then ends it. I put some HTML and PHP code between the start and end of the timer.

The script looped 99999 times, and created a drop down item for each loop. I did one the way I like to code, and the other with HTML within PHP tags.

Now, this isn’t conclusive evidence but, through all of my tests, the code with HTML within PHP ECHO’s took longer to be generated on the server. Although there wasn’t much difference in it (1000ms at most), there was a difference, and on a big project it could be wasted processor usage.

What I find puzzling as well is, if you put HTML within an ECHO, then you’re using processor usage to echo out the HTML. When it gets to the browser, the browser uses processor usage to render the page. So essentially, you’re parsing the same code twice…for no reason whatsoever, and with absolutely no benefit.

Save server resources and code correctly I say.

My biggest issue with echoing HTML is it’s a lot harder to main and update.

The browser has to parse it no matter what you do, so that is irrelevant.

The server processor is used no matter whether you echo or whether the html is used outside of <?php tags or not. Keep in mind, php is parsing every character in the file regardless. It needs to find its opening and closing tags. The webserver doesn’t pre parse the file and send the blocks of php code to php. Php is sent a filename, and php is responsibe for loading the file and returning a result to the webserver. This means php reads the file from disk and parses it in its entirety.

But yes, outputting strings which are outside of <?php tags is often a bit faster than using echo. The difference is generally insignificant, espescially if using echo on single quoted strings. To do a fair comparison, a single echo should be used(probably spanning multiple lines). Multiple calls to echo would only be fairly compared to multiple breaking in and out of php tags.

Double quoted strings take longer to parse than single quoted strings, because php recognizes more escape sequences in double quoted strings, as well as having to do variable expansion.

Output buffering settings have a large effect on the speed of all of this.