You are here: Re: [PHP] php style guides « PHP « IT news, forums, messages
Re: [PHP] php style guides

Posted by Jochem Maas on 10/04/92 11:05

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?
>
> I generally have my own preference for formatting like
>
> if ( condition )
> {
> statements;
> }

which personally I think is ugly and a waste of space ;-)
(actually I used to do it like that but the better I got at programming
the more I found myself trying to condense my code - mostly so that I
could fit more of it on screen.)
currently I reserve the above CS for functions and classes.

e.g.

myCoolFunc( $arg1, $arg2 = 'default' )
{
// do something cool!
}

>
> for all conditional statements and loops, as opposed to
>
> if (condition) {
> statements;
> }

this is my (exact) preference for block statements.

its all very personal this!

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

which very quickly gets completely unreadable when the complexity starts
piling up.

>
> but just tried
>
> <?php
> echo 'blah';
> echo '<h1>test</h1>';
> echo 'blah';
> ?>

much easier to read, but what about:

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

or (where the literal strings would normally be vars):

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

or (the semi-colon can be dropped here):

<?= 'blahh1>test</h1>blah'; ?>

the consensus is (AFAIKT) that the less you break in and out of PHP
the more legible your code is likely to be. In the same vein it is often
suggested to first process/gather you data and then output everything
after your script is done doing its thing, rather than echoing as you go.

there is also the HEREDOC style of echoing output (actually its a method
of string delimitation) e.g.:

<?

echo <<< THE_END
yadda yadda yadda $myArr['key1'][$key2]
$jumpup, $howhigh
THE_END;

?>

read more here (or google PHP+HEREDOC):
http://nl2.php.net/types.string

>
> 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 always advise that readability/maintainability is king - speed is
hardly ever an issue on an average php site, and when speed becomes
critical optimizing echo statements is probably not where you want to
start (creating objects and calling functions is way,way heavier).

so in short: do what you find to be the easiest to read back in 6 months
time!

>
> Thanks for your time.
>
> Tim
>

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация