Re: php style guides

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

 



Tim Burgan wrote:
Hi everyone,

What 'rules' do you follow about styling/formatting your PHP code? Do you follow a guide that is available online?

<sacred_cow_warning>Please do not flame me here, different strokes for different folks!</sacred_cow_warning>


If you're entirely new to coding in PHP ... and you don't have a codebase already existing... then you might consider using the coding standards as outlined by PEAR (apparently several other people agree):
http://pear.php.net/manual/en/standards.php



I generally have my own preference for formatting like

if ( condition )
{
   statements;
}


And that's fine. The main thing is: aim for consistency of your coding standards. That way when you look through an entire project it's easy to see what's going on.


for all conditional statements and loops, as opposed to

if (condition) {
   statements;
}

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";
?>


Personally I don't mix and match HTML with my PHP code. I am extremely lazy and enjoy finding PHP code available that will do all of the echo'ing for HTML I want to build. The PEAR package HTML_Quickform is a good example here; it solves a common problem (building an MVC form) for me in a way that is faster than coding it all out by hand. That is, once you know how to use the package. (There are loads of other packages out there as well; this is but one example.)


But if you look at your HTML as a template (which it pretty much is) for your front end and you view PHP as your logic back-end (which it is) then mixing them together is fine. Separating presentation and logic is generally a good idea; this way you can easily change the way it looks without breaking functionality (or vice versa). Although if/when I do it this way I try to make it pretty obvious what I'm trying to accomplish.

<html>
<head><?php include 'functions.php'; ?></head>
<body>
  <h1><?php getHeader1(); ?></h1>
  <h2>Hello <?php getUserName(); ?>!  How are you doing this fine day?</h2>
</body>
</html>

but just tried

<?php
  echo 'blah';
  echo '<h1>test</h1>';
  echo 'blah';
?>

which I find it's much easier to read to code.

That's always an important consideration. :)


What do other people do and for what reason? What are the advantages/disadvantages to doing it certain ways?


Thanks for your time.

Tim


--
Teach a person to fish...

Ask smart questions: http://www.catb.org/~esr/faqs/smart-questions.html
PHP Manual: http://www.php.net/manual/en/index.php
php-general archives: http://marc.theaimsgroup.com/?l=php-general&w=2

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