Re: Buxa Coding Guidelines

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Per Jessen wrote:
magoo wrote:

In their oppinion stuff like the short PHP init "<?" or inline echo
"<?= $var ?>" is forbidden.
Do you people code that strict?

Yep.  If you're using XHTML you have no other option.


If you're parsing XHTML that is choking on any instances of "<%" then you've got a problem with your script. The point of including PHP tags inside of an "XHTML" file is that it isn't really XHTML until PHP has parsed it for its own instructions. Until then, it isn't XHTML yet and shouldn't be treated as such. If you've some need to treat the raw files as XHTML on the server *before* it reaches PHP then you're doing something wrong.


Greg Donald wrote:
>
> I don't put PHP in my HTML.  I use PHP to build my HTML, using heredoc
> syntax while doing so.  My scripts usually only have a single <?php
> opening tag and never require any closing ?> tags.

You make it sound as if you've saved yourself having to type 2 characters. If you're using Zend or a template engine and your scripts do not contain anything other than PHP then you *should not* include the closing tag. It's not really a choice (well, it is, but you run the risk of creating difficult-to-track bugs).

It all depends on the context. For example:

Say i turn an existing HTML file into a PHP script and add some instructions somewhere within the middle of the body. Of course i'm going to have to use a closing tag somewhere before the next bit of HTML markup. But i certainly don't care that my script *requires* that i use that closing tag. I don't think, "Gee, i wish i didn't have to put this closing tag here."


> Code that contains short open tags or instances of <?= is amatuer in
> my opinion.

Amateur? I agree that short open tags should be avoided but i don't see anything wrong with using the <?= ?> construct. When a full-blown templating engine is not available it can be a huge help to break down pages into seperate portions, each containing instructions for the PHP engine to fill in. Got a script that displays a form on one request and something different on another? It's easy to simply include() the necessary HTML part from another file, complete with <?= ?> instances to fill in the stuff you want PHP to gather.

Hence, a poor man's template. I open the file and see an HTML form and can follow it easily. I don't have to wade through a bunch of heredoc blocks in the main script. Instead, the script where i'm doing all the heavy lifting is empty of presentational markup that would only get in the way.

I find it amusing when people go on and on about keeping their PHP instructions out of the markup yet have no qualms about doing precisely the opposite. Six of one ...

It's a matter of determining what is the model and what is the view and how best to create an application structure.

And using echo or heredoc to create HTML usually creates unreadable markup, IMO. That may not matter to some but for those of us who are responsible for ensuring pages validate it certainly helps when your editor of choice (i use jEdit) can highlight the syntax--both PHP and HTML--properly. Markup stuck in a heredoc (or ~shudder~ a thousand echo statements) isn't so easy on the eyes.

In these "templates"i use, aside from filling in spots with <?= $my_var ?> i may also have the odd loop to iterate over a database result. Not so different from XSL, in fact. But i don't have all of the DB queries, checking of credentials, class definitions (yes, i've seen it: classes defined in the same 10,000-line script that the browser requests) inside of that file. It wouldn't be anything remotely similar to a template then.

There's certainly a case to be made for ensuring that scripts follow certain guidelines but to assert that there is *one* proper way for all cases seems a bit off to me. Use what works, makes sense, and is *safe* within *your* workflow.

Just thought i'd add something to get the discussion going beyond, "Me too! That way is teh suxxor." ;-)

brian

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux