Re: [PHP] Buxa Coding Guidelines



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
.



Relevant Pages

  • Re: How do we get there from here?
    ... server-side-scripted html. ... This is a simple example with very little php scripting. ... means that the version of the php pre-processor on your web server must ... >>> The browser never sees anything not sent to it by the script. ...
    (comp.databases.pick)
  • Re: how to run scripts after a page has already loaded and been sent to a users browser?
    ... > It's because PHP is a server side scripting language, ... > do stuff before you send stuff to user's web browser. ... As we've discussed on this newsgroup before, if a script starts ... that is after the last HTML is sent to a web browser. ...
    (alt.php)
  • Re: [PHP] Couple of beginner questions
    ... The custom tag will expand the path to wherever the images directory was ... It's a pain in the ass overriding a CSS rule that was ... wherever I can't simply redefine the html tag. ... infact ideally an html tag should never be seen in a php script ...
    (php.general)
  • Re: How do we get there from here?
    ... Unlike my dislike for PHP, my dislike for SQL is based on ... >>> up with at least html and php quite mixed into each other, ... Using a PHP script to send HTML ... code to a browser in no way taints that code. ...
    (comp.databases.pick)
  • Re: Form Data -> Variables or an Array?
    ... Forgive me I am a PHP newbie. ... I have a small script that enables me to ... I want to use the HTML formatted form ... variables or an array, so that I can call the data later in the same ...
    (comp.lang.php)