Closed Thread Icon

Topic awaiting preservation: php/html syntax Pages that link to <a href="https://ozoneasylum.com/backlink?for=27149" title="Pages that link to Topic awaiting preservation: php/html syntax" rel="nofollow" >Topic awaiting preservation: php/html syntax\

 
Author Thread
redroy
Paranoid (IV) Inmate

From: 1393
Insane since: Dec 2003

posted posted 12-15-2005 04:25

Is there any reason I wouldn't want to do something like this

code:
<?php if ($someVar == "something") { ?>

html 'n stuff

<?php } else { ?>

other html 'n stuff

<?php } ?>

as opposed to using echo?



(Edited by redroy on 12-15-2005 04:26)

DL-44
Maniac (V) Inmate

From: under the bed
Insane since: Feb 2000

posted posted 12-15-2005 05:04

Whenever posible, I will end my PHP and write regular HTML rather than using echo.

The main reasons are

1) clear definition. 'this is php'. 'this is html'. Makes editing and error spotting much easier.

2) with large chunks of html, echo becomes rather cumbersome - having to escape your characters, not having the benefit of proper syntax highlighting in my code editor because it's inside quotes, etc.

Obviously many circumstances don't allow such separation, but when it is an option, and I usually use it.

{{edit - Slightly misread the actual question.

When the code is within a page that will be rendered as HTML, no, there is no reason not to keep your HTML and PHP separate, and in fact I would recommend it.
I generally reserve echo for code within functions and other such situations.




(Edited by DL-44 on 12-15-2005 05:08)

DmS
Maniac (V) Inmate

From: Sthlm, Sweden
Insane since: Oct 2000

posted posted 12-15-2005 11:22

I'll take the opposite & harsh opinion...

You would use this if you want to create spaghetti code that will be very hard to extend over time

Ok, ok, there are exceptions but basically as I see large blocks of PHP logic mixed with blocks of HTML I cringe, get stomach aces and I close the editor...


For me, the only way to do this (and yes I use it) is when you create a template page that holds variables containing dynamically generated non-formatted content, or function/method calls returning non-formatted content that should be displayed within the template.

All backend-logic except simple if() or loops should be as far away from the presentation code as possible in my opinion!

If you absolutely need some logic in the template, place it before the HTML and create variables that you can use in the HTML.

Keep the presentation clean and you will benefit from it in the long run, I promise.
/D

{cell 260} {Blog}
-{"Theories without facts are just religions...?}-

DL-44
Maniac (V) Inmate

From: under the bed
Insane since: Feb 2000

posted posted 12-15-2005 13:45

I wouldn't say that is the opposite opinion, just a stricter view of it

The question seemed, to me, to be *if* if you are going to have your PHP and HTML mixed, which way to go about it? And the answer to that, for me, is that it is best to keep the HTML out of the PHP tags (long strings of echos full of HTML is worse than alternating blocks of php/html, no?).

DmS
Maniac (V) Inmate

From: Sthlm, Sweden
Insane since: Oct 2000

posted posted 12-15-2005 18:11

Ok, I'll go for stricter

quote:
(long strings of echos full of HTML is worse than alternating blocks of php/html, no?).



Yes, it's harder to maintain, in the rare cases I've had to push out lot's of HTML inside php and normally would have used echo/print I've used HEREDOC syntax instead.
http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc

This way you can collect blocks of complex HTML inside a function/class separated from the HTML, store it in a variable, return that and use echo/print in the template.

I did this when I made a php class to easily integrate one or several instances of a prebuilt wysiwyg in templates directly through php. Wasn't really pretty but works just fine.

Personally I'd look more into that than jumping in and out of php inside html.
Just feels more flexible, plus, should you need to change the structure of the html it will change in all places it's used which can't be all bad
/D

{cell 260} {Blog}
-{"Theories without facts are just religions...?}-

(Edited by DmS on 12-15-2005 18:13)

DL-44
Maniac (V) Inmate

From: under the bed
Insane since: Feb 2000

posted posted 12-15-2005 21:39

I'm not sure I fully grasp the use of heredoc - I'll have to read through that again when I have more time.

I'm also a little behind on my understanding and implementing of classes....still digesting a lot of the info from your GN tutorial

I always appreciate the info dms =)

Tyberius Prime
Paranoid (IV) Mad Scientist with Finglongers

From: Germany
Insane since: Sep 2001

posted posted 12-15-2005 21:41

Personally, these days I use a smarty based presentation layer.

That works like a charm for all those smaller projects - and if you're in the range where smarty is to slow, you'd be better using 'static' pages anyhow)

« BackwardsOnwards »

Show Forum Drop Down Menu