Re: php style guides

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

 



* Tim Burgan <email@xxxxxxxxxxxxx>:
> What 'rules' do you follow about styling/formatting your PHP code? Do 
> you follow a guide that is available online?

PEAR standards are fairly well-accepted:
http://pear.php.net/manual/en/standards.php

> I generally have my own preference for formatting like
>
> if ( condition )
> {
>     statements;
> }
>
> for all conditional statements and loops, as opposed to
>
> if (condition) {
>     statements;
> }

PEAR actually recommends this latter instead of the former; the only
case they make for opening braces on the following line is with class
and function/method definitions. (I personally don't understand why
these are treated differently, but I'm sure there's some discussion in
the mailing lists that treats it.)

> But what I'm really wanting to get everyones thoughts about is in regard 
> to combining PHP with HTML.
>
> I used to do:
>
> <?php
>    echo "blah";
>    ?><h1>test</h1><?php
>    echo "blah";
> ?>
>
> but just tried
>
> <?php
>    echo 'blah';
>    echo '<h1>test</h1>';
>    echo 'blah';
> ?>
>
> which I find it's much easier to read to code.
>
> What do other people do and for what reason? What are the 
> advantages/disadvantages to doing it certain ways?

I personally try not to put HTML into my code, and use templates:

* Placing HTML into PHP means I'm having to try and decipher two
  languages (or more, if I use CSS or Javascript) in the same file;
  making the mental switch in the same file is typically more difficult
  than having HTML/CSS/JS in different files.

* If I need to worry about HTTP headers deep into the program logic (for
  instance, to perform a redirect or set a cookie), I don't want to
  worry about whether or not any HTML has been sent to the screen.

* If I need to fix a typo in the HTML, I don't want to browse through a
  bunch of PHP to find it.

When I *do* place HTML in my PHP (it *does* happen occasionally), I
typically use heredocs so I don't have to worry about quotes and such:

    $html =<<<EOH
    <h1>Test</h1>
    <p>This is a test of the emergency HTML situation</p>
    <p>It received $error_signal</p>
    EOH;

However, all the above is my personal coding preference; I've heard very
good arguments for other styles and for not using templates.

-- 
Matthew Weier O'Phinney           | mailto:matthew@xxxxxxxxxx
Webmaster and IT Specialist       | http://www.garden.org
National Gardening Association    | http://www.kidsgardening.com
802-863-5251 x156                 | http://nationalgardenmonth.org

-- 
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