473,320 Members | 2,111 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

echo or ?><?php

I have to output a lot of html stuff mixed with php and I'm curious if its
better to echo the html code or stop the php parser temporarily to got into
"html mode"? Is there any drawbacks to either method?

e.g.,

echo "<div class=\"someclass\">"

or

?><div class="someclass"><?php

(realize that this is just a simple example and is not meant to represent
the actual code I will be writing)

Thanks,
Jon
Jun 13 '07 #1
9 1941
On Jun 13, 7:23 pm, "Jon Slaughter" <Jon_Slaugh...@Hotmail.comwrote:
I have to output a lot of html stuff mixed with php and I'm curious if its
better to echo the html code or stop the php parser temporarily to got into
"html mode"? Is there any drawbacks to either method?

e.g.,

echo "<div class=\"someclass\">"

or

?><div class="someclass"><?php

(realize that this is just a simple example and is not meant to represent
the actual code I will be writing)

Thanks,
Jon
In practice it won't really make any noticable difference. Do
whatever makes the code more readable and easier to maintain.

Jun 14 '07 #2
On Jun 13, 8:23 pm, "Jon Slaughter" <Jon_Slaugh...@Hotmail.comwrote:
I have to output a lot of html stuff mixed with php and I'm curious if its
better to echo the html code or stop the php parser temporarily to got into
"html mode"? Is there any drawbacks to either method?

e.g.,

echo "<div class=\"someclass\">"

or

?><div class="someclass"><?php

(realize that this is just a simple example and is not meant to represent
the actual code I will be writing)

Thanks,
Jon
i think it's better to stop the parser and write the html...
....?>
<div class="someclass">
<?=$MyString?>
</div>
<?...

it's easy to see the html and the php code...

Jun 14 '07 #3
..oO(SrSilveira)
>i think it's better to stop the parser and write the html...
...?>
<div class="someclass">
<?=$MyString?>
</div>
<?...
Are you sure short_open_tag will be enabled on all of your servers?

Micha
Jun 14 '07 #4
Jon Slaughter wrote:
I have to output a lot of html stuff mixed with php and I'm curious if its
better to echo the html code or stop the php parser temporarily to got into
"html mode"? Is there any drawbacks to either method?

e.g.,

echo "<div class=\"someclass\">"

or

?><div class="someclass"><?php

(realize that this is just a simple example and is not meant to represent
the actual code I will be writing)

Thanks,
Jon

Well, a couple things...

1) echo "<div class='someclass'>" is perfectly acceptable afaik as well
as the reverse

2) the HEREDOC format also works well, examples:

$str = <<<EOT
<HTML>
<HEAD>
<TITLE>$pagetitle</TITLE>
</HEAD>
<BODY>
...
</BODY>
</HEAD>
EOT;

or

echo works too, and variables will be resolved following the same rules
as double quoted strings.

As to what is better??? Me personally, I don't use escape characters
or do alot of string concatination at all if I can get away with it...
too hard on the eyes. I would imagine jumping in and out of the parser
to be equally hard on the eyes depending on how much you have to do it.
Again, personally, I use a template system (not Smarty) for that type of
output. Code the way things are comfortable for you but remember,
outside the parser no variables can be used.

Norm
Jun 14 '07 #5
Jon Slaughter wrote:
I have to output a lot of html stuff mixed with php and I'm curious if its
better to echo the html code or stop the php parser temporarily to got into
"html mode"? Is there any drawbacks to either method?

e.g.,

echo "<div class=\"someclass\">"

or

?><div class="someclass"><?php

(realize that this is just a simple example and is not meant to represent
the actual code I will be writing)

Thanks,
Jon

Jon,

To me, it all depends. If it's something short, I'll generally just
echo it. If it's longer, I'll get out of PHP mode.
As ZedorBlat indicated, you won't notice any real performance impact one
way or the other. To me, it's all about readability.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jun 14 '07 #6
On Jun 13, 10:01 pm, Michael Fesser <neti...@gmx.dewrote:
.oO(SrSilveira)
i think it's better to stop the parser and write the html...
...?>
<div class="someclass">
<?=$MyString?>
</div>
<?...

Are you sure short_open_tag will be enabled on all of your servers?

Micha
of course i'm sure. if it's not enabled you can just do like that
<?php=$Mystring?>
</div>
<?php...
Jun 14 '07 #7
..oO(SrSilveira)
>On Jun 13, 10:01 pm, Michael Fesser <neti...@gmx.dewrote:
>Are you sure short_open_tag will be enabled on all of your servers?

of course i'm sure. if it's not enabled you can just do like that
<?php=$Mystring?>
Still wrong. If you want reliable and portable code, then it has to be

<?php echo $MyString?>

short_open_tag is entirely optional. You should only use it if you are
_always_ in control of the servers your scripts are supposed to run on
or at least be able to change their configuration. It's still bad style
and not recommended.

Micha
Jun 14 '07 #8
echo "<div class=\"someclass\">"
>
or

?><div class="someclass"><?php

I would go for
echo '<div class="someclass">

<p>'. $some_variable_here .'</p>

</div>';

but also, I like the idea of separating the code from the HTML by
using templates or simply

<div class="someclass">

<p><?php echo $some_variable_here; ?></p>

</div>

Both look easy and simply to ....read.
Cheers
Vladimir

Jun 14 '07 #9
On Wed, 13 Jun 2007 18:23:42 -0500, in comp.lang.php "Jon Slaughter"
<Jo***********@Hotmail.com>
<V%*******************@newssvr23.news.prodigy.netw rote:
>| I have to output a lot of html stuff mixed with php and I'm curious if its
| better to echo the html code or stop the php parser temporarily to got into
| "html mode"? Is there any drawbacks to either method?
|
| e.g.,
|
| echo "<div class=\"someclass\">"
|
| or
|
| ?><div class="someclass"><?php
|
| (realize that this is just a simple example and is not meant to represent
| the actual code I will be writing)
I think it would depend upon how you have your file structured.

I place most of the php processing at the top of the file an
intermingle the necessary php code within the html section.

php block
html
styles block
javascript block
body
<p class="<?php echo $x;?>">some text</p>
<select name="lbOptions">
<?php for($x=0; $x<count($records); $x++)
echo "<option value='" . $records[$i]->ID . "'>" .
$records[$i]->info . "</option>";
?>
</select>
</body>
</html>

But then again, when I'm using AJAX calls I usually have the html
embedded within the php code.

Also, using a text editor with syntax highlighting is a must.
---------------------------------------------------------------
jn******@yourpantsyahoo.com.au : Remove your pants to reply
---------------------------------------------------------------
Jun 15 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: js | last post by:
I have a table rendered with XSLT. The first column has a radio button controls for user to make a selection for a particular row. All the values in the remaining columns are all concated with a...
4
by: Michael G | last post by:
Some of the online docs say that you can use echo <<<END all sorts of text and statements... END; but if the following produces a parse error at line 14 which is the closing
7
by: Rocky Moore | last post by:
I have a web site called HintsAndTips.com. On this site people post tips using a very simply webform with a multi line TextBox for inputing the tip text. This text is encode to HTML so that no...
14
by: Khai | last post by:
I'm an extreme newbie, and have been nosing around the PHP.Net site for a few days. Only, the search function returns alot of information that's not truly relevant due to Coding samples at the...
5
by: Nathan Sokalski | last post by:
My Web.config file contains the following section to register some of my UserControls: <pages> <controls> <add tagPrefix="NATE" tagName="Banner" src="~/Banner.ascx"/> <add tagPrefix="NATE"...
14
by: Michael | last post by:
Since the include function is called from within a PHP script, why does the included file have to identify itself as a PHP again by enclosing its code in <?php... <?> One would assume that the...
7
by: Nathan Sokalski | last post by:
Something that I recently noticed in IE6 (I don't know whether it is true for other browsers or versions of IE) is that it renders <br/and <br></br> differently. With the <br/version, which is what...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.