473,406 Members | 2,439 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,406 software developers and data experts.

Is it better to echo or leave as html?

I have a multi step signup form that I'm building and I have the
following question. Should I do it this way:

<?php

if (blah == 1) {
?><table><td width="100">........etc
<?php } else { ?>
<table><td width="100">........etc
<?php } ?>

OR

<?php

if (blah == 1) {
echo "<table><td width=\"100"\>........etc";
} else
echo "<table><td width=\"100"\>........etc";
}?>


If the second way is better is there an easier way to convert the HTML
rather then writting echo a million times by hand?

Jul 17 '05 #1
8 2475
On 30 Sep 2003 17:24:17 -0500, Nick Messick <no****@trendwhore.com> wrote:
I have a multi step signup form that I'm building and I have the
following question. Should I do it this way:

<?php

if (blah == 1) {
?><table><td width="100">........etc
<?php } else { ?>
<table><td width="100">........etc
<?php } ?>

OR

<?php

if (blah == 1) {
echo "<table><td width=\"100"\>........etc";
} else
echo "<table><td width=\"100"\>........etc";
}?>


It's a matter of personal preference, and depends on what you're outputting.

I prefer the first one; drop out of php and output literal HTML, unless it's a
very short output within a much greater number of lines of PHP.

I think an improvement on the second option is instead of:

echo "<table><td width=\"100"\>........etc";

Use single quotes in the HTML inside:

echo "<table><td width='100'>........etc";

This reduces the number of slashes, making it much easier to read.

--
Andy Hassall (an**@andyh.co.uk) icq(5747695) (http://www.andyh.co.uk)
Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)
Jul 17 '05 #2
Andy Hassall wrote:
Use single quotes in the HTML inside:

echo "<table><td width='100'>........etc";

This reduces the number of slashes, making it much easier to read.


i'm not sure about this. It will reduce number of slashes, but will it
be W3C compliant?

Jul 17 '05 #3
On Wed, 01 Oct 2003 01:34:39 +0200, "dr. zoidberg" <so*****@example.wrong>
wrote:
Andy Hassall wrote:
Use single quotes in the HTML inside:

echo "<table><td width='100'>........etc";

This reduces the number of slashes, making it much easier to read.


i'm not sure about this. It will reduce number of slashes, but will it
be W3C compliant?


Yes.

http://www.w3.org/TR/html4/intro/sgmltut.html#h-3.2.2

"By default, SGML requires that all attribute values be delimited using either
double quotation marks (ASCII decimal 34) or single quotation marks (ASCII
decimal 39)."

--
Andy Hassall (an**@andyh.co.uk) icq(5747695) (http://www.andyh.co.uk)
Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)
Jul 17 '05 #4
"Nick Messick" <no****@trendwhore.com> wrote in message
news:3f***********************@newscene.com...
I have a multi step signup form that I'm building and I have the
following question. Should I do it this way:


There are people who prefere it first way, and there are people who prefere
it second way. I personaly prefere it when html and code are in completely
separate files.

rush
--
http://www.templatetamer.com/

Jul 17 '05 #5
Hi Rush,
I personaly prefere it when html and code are in completely separate

files.

Don't forget that a Template Engine is not supposed to separate PHP code
from HTML but business logic from presentation logic.

Just to evangelize you : http://www.sitepoint.com/article/1218

JM ~ an angel passes by ~

--
Clé AntiPourriel : PASUNPOURRIEL (ne pas retirer)
Jul 17 '05 #6
First way. Because embedding HTML in a echo is a real pain.

From the manual : "PHP is scripting language that is especially suited for
Web development and can be embedded into HTML"

"embedded", it's the magic word. It means PHP is meant for that, you need
PHP, use <?php Some PHP code... ?>, you don't it anymore ? Escape from PHP
to HTML : ?>Some HTML...<?php... You just need to output the value of a PHP
variable ? Use the echo shortcut : <?= $my_php_var ?>... And voilà :)

Hope it helps,
JM

--
Clé AntiPourriel : PASUNPOURRIEL (ne pas retirer)
Jul 17 '05 #7
"Jean-Marc Molina" <go****************@ifrance.com> wrote in message
news:bl**********@news-reader5.wanadoo.fr...
Hi Rush,
I personaly prefere it when html and code are in completely separate

files.

Don't forget that a Template Engine is not supposed to separate PHP code
from HTML but business logic from presentation logic.


There are different school of thoughts on this, and I do not prefer the one
you propose, while I accept that it works for you and that you prefer it.

I think Template engine purpose _is_ to separate html from code. And that
bussiness logic shoud be separated from presentation logic by organizing
your code in MVC, or MVP patterns where business logic goes into M. But it
is the code part that separates presentation logic and bussines, not
templates which are used to separate html which define pure visial
appearance, and code defines presentation logic. It is much clear in
languages that come with MVP out of the box, like Dolphin Smalltalk. There
you have View composer, where you paint the appearance of the GUI and it
gets stored as a resource. It is equivalent of html. Than there is
Presenter, which is pure code and contains "Presentation" logic. And there
is Model that contains bussines logic.

The idea that template purpose is to separate business logic from
presentation has lead to "dirty" templates which contain both code and html.
In this scenario code (not necessary a php code, but code with loops, ifs
and other constructs anyway), is embeded into the html template. Most
prominent and respected examples of such approach would be XML+XSLT and
Smarty.

Again, both approaches work and some people like one, and some people like
onother, but it is by no means such a clear cut, and just a single "proper"
way to approach things.

Thanks,

rush
--
http://www.templatetamer.com/

Jul 17 '05 #8


Use single quotes in the HTML inside:

echo "<table><td width='100'>........etc";

This reduces the number of slashes, making it much easier to read.


Or rather, if (like myself) you don't like using ' for quoting attributes
in HTML, use:

echo '<table><td width="100">........etc'
This only becomes a problem if this needs to contain a variable, in which
case you do:

echo '<table><td width="100">' . $variable . '......etc'

Double-quotes in PHP mean that PHP parses (looks through) the quoted
string for variales and PHP-specific things like \n or \r

Single quotes doesn't - it just dumps the result back, and so is
theoretically quicker.

This applies in all sorts of cases, and the PHP documentation often uses "
even when not necessary.

You should also consider doing this instead of echoing:

$html = "\n" . '<table><td width="100">'
$html .= "\n<tr>";
#etc
$html .= "\n</table>";

echo $html;
I.e. build up your HTML then when it's complete, show it. This has the
benefit of not producing half-complete HTML if your script finds a problem
half-way through. It also makes printing error messages more flexible,
because at the end you could have a check which then overwrites the
built-up HTML with the warning(s), etc. - if you've already echo'd the
result, you don't have that choice, and you have to do *all* the checking
at the start.
Martin Lucas-Smith www.geog.cam.ac.uk/~mvl22
www.lucas-smith.co.uk
Jul 17 '05 #9

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

Similar topics

7
by: John | last post by:
I have over 5000 thumbnail pictures of size 5kb each. I would like to able to load all 5000 pictures and view 50 per page using mysql_data_seek(). I would like to know what are the advantages and...
6
by: Marco | last post by:
I have a couple pages that have tables, whats best use echo to produce the full table with the variables or is it best to just make the table in plain html and use <?php echo ('$va'l); ?> when i...
6
by: Michael | last post by:
Hi, A quick and most likely simply question about which is generally better programming with PHP. Is it more proper to break out of PHP code to write HTML, or is it ok rto do it within print()...
2
by: peridian | last post by:
I've gotten a really weird result crop up on my site, and I can't find any information on the web that it has been seen elsewhere. Given how weird this is, I guess it's probably a symptom of either...
2
by: L. Berger | last post by:
Hi, I am working on an HTML template which has a lot of html tags, with PHP data shown in the middle of these tags -- you know, the usual. Currently, I have HTML as is, and many many "echo...
32
by: Request-1 | last post by:
hi folks, html coder here, new and terrified * of php!! aaaaaa! i'm trying to bury a JS script to rotate a photo, in a page i converted from html to php. the conversion went well, it was to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.