Re: Embedding HTML Within a PHP Statement [message #175984 is a reply to message #175980] |
Mon, 14 November 2011 10:39 |
Tim Streater
Messages: 328 Registered: September 2010
Karma:
|
Senior Member |
|
|
In article <mhg1c7p6qe5ng95rfiguu1b0tu18143m4a(at)4ax(dot)com>,
Call Me Tom <noemail(at)nowhere(dot)com> 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. Today I found the following code in a working program:
>
> <?php if (!empty($error_message)) { ?>
> <p class="error"><?php echo $error_message; ?></p>
> <?php } ?>
>
> 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) but rather
> 1. Does this only work in special cases or does it work in all
> cases?
> 2. Is it considered good programming, bad programming or personal
> preference?
Perfectly valid. I used to use that approach, but I find it makes it too
hard to keep it clear what I am doing. I have separate PHP, html, and JS
files. I've given up on using php to directly build pages in that way.
What I do instead is to use ajax to collect some data from a php script
and then use JS to interpret that data and adjust the page as necessary.
E.g. you might imagine that the user makes a selection. Instead of
submitting the page and then presenting them with an error message, far
better IMO to have a <div> that is normally empty, use ajax to send the
user's request to a PHP script. The PHP script attempts to perform the
user's request and then returns a results message (based on
success/failure, for example), that you can present in the <div> using
JavaScript. Much cleaner and much better from the user's PoV.
--
Tim
"That excessive bail ought not to be required, nor excessive fines imposed,
nor cruel and unusual punishments inflicted" -- Bill of Rights 1689
|
|
|