FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Embedding HTML Within a PHP Statement
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: Embedding HTML Within a PHP Statement [message #175983 is a reply to message #175980] Mon, 14 November 2011 10:10 Go to previous messageGo to previous message
Erwin Moller is currently offline  Erwin Moller
Messages: 228
Registered: September 2010
Karma:
Senior Member
On 11/14/2011 8:29 AM, Call Me Tom wrote:
> I was under the impression that a section of PHP code (the part
> between<?PHP and ?>) was independent of any other section of PHP
> code.

Have you never seen constructs like these before?
<table>
<tbody>
<?php
foreach ($someDBResult as $oneRow){
?>
<tr>
<td>
<?php echo $oneRow["firstName"]; ?>
</td>
<td>
<?php echo $oneRow["initials"]; ?>
</td>
</tr>
<?php
}
?>
</tbody>
</table>

I bet you have seen that before.


> Today I found the following code in a working program:
>
> <?php if (!empty($error_message)) { ?>
> <p class="error"><?php echo $error_message; ?></p>
> <?php } ?>
>

Perfectly legal PHP code.


> This is the first time I have seen a section of PHP code end in the
> middle of a PHP statement and restart after some HTML was inserted. I
> was surpised that the PHP interpreter was able to connect the two
> sections of PHP code.
>
> My question is not how the interpreter does it (that's way beyond my
> level of knowledge)

No, it is not that hard to figure out.
Think in levels of nesting.
But I bet it was hard to implement for the developers. ;-)


> but rather
> 1. Does this only work in special cases or does it work in all
> cases?

It always worked as expected for me.

> 2. Is it considered good programming, bad programming or personal
> preference?

I call it personal preference.
When you have LOADS of conditional statements and loops, it is clearly
the time to clean up the code a bit, but that goes for each piece of
code, in any language.

When you are inside complex conditional structures, and you do not want
to restructure the code, the opening and closing of PHP tags will add
extra confusion, and using echo instead might help a little to keeps
things understandable (for humans).

The way I see it, when comparing the 2 approaches:
Dropping in and out:
<?php if (!empty($error_message)) { ?>
<p class="error"><?php echo $error_message; ?></p>
<?php } ?>

Compare to using echo:
<?php
if (!empty($error_message)) {
echo "<p class=\"error\">".$error_message."</p>";
}
?>

The former has the advantage that you can use literal HTML.
No escaping needed like in echo "".
The good thing about using echo instead is that you have a lot less
<?php and ?> messing up your code, which clearly reduces the readability
of that code.

You I think it is your own preference.

Personally I let my choice depend on the situation.
When I work on a project where PHP code and lay-out are NOT separated,
and the design-people will modify my PHP files (mostly adding
style-details), I prefer to drop in and out of PHP, so they don't have
to worry about escaping.

When you are the only one using the code, do whatever YOU think is the
most clear way: Escaping versus tag-soup.

Of course, when you use some templating system, the problem with
designers messing with your code mostly vanishes and the choice becomes
totally your preference.

just my 2 cent.

Regards,
Erwin


>
> Thanks for some insight,
> Tom


--
"That which can be asserted without evidence, can be dismissed without
evidence."
-- Christopher Hitchens
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Smart File Downloader - simple script on xampp
Next Topic: Prevent unlink(...) warning
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Mon Nov 25 04:56:38 GMT 2024

Total time taken to generate the page: 0.04884 seconds