Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP Coding Standards

Status
Not open for further replies.

cmhunt

Programmer
Apr 17, 2001
119
GB
Hi

I am new to PHP and have been teaching myself from tutorials off the internet. Having got a decent idea of what is going on, I was wondering if anyone knew the location of a set of sensible set of PHP coding standards.

Also, a simple efficiency question, if I have a large csv being read in to an array which I am then displaying in a table, is there any significant efficiency differences between:

<?
print &quot;<td>&quot;;
print $data[13] . &quot;\n&quot;;
print &quot;</td>&quot;;
?>

and:

<td>
<?=$data[13]?>
</td>


Thanks in advance for any help.

Chris
 
Coding standards are locally defined.

I've been wondering about that very question for more than a year, and have never been able to find any pages on that subject.

I became curious because I know that in VBScript/ASP, context switching is very expensive and wondered whether the same was true in PHP. My own experiments haven't been conclusive

I stick with the non-context-switching style just in case. And for esthetics, too.

Want the best answers? Ask the best questions: TANSTAAFL!
 
i think that articles & tutorial on zend (zend.com) could help you on coding standards. See for example &quot;Top 21 PHP progamming mistakes&quot; ( i bet it's reallly close to what you're looking for.
now, if you're really interested in going (a weeee bit) faster, you can benchmark your code yourself. Either use your own function (example in the &quot;echo&quot; function comments on php.net ( -cs7536 is right: php.net is *ALWAYS* THE place to go) or use a library that will help you do so (google might find one for you ;) )
finally, there is an optimizer ( - but i've never tried it and i don't know if it's worth it
 
As a side note on optimization of your posted code (context switching aside)....

I stumbled across some IBM PHP optimization article awhile back which &quot;suggested&quot; that using single quotes for static strings should be slightly faster. The logic is that double quotes are parsed to expand variables and special characters (like \n), whereas single quotes are copied verbatim.

Somehow I doubt very many scripts exist which you'd notice the switch, but it's something to consider if you're really interested.

Hope you've enjoyed the completely useless commentary.

-Rob
 
Thanks everyone.

I have done a lot of searching through php.net in the past and not found these answers.

I understand Coding standards are defined locally but are not easy to start documenting when you are fairly new to php and I was looking for a starting place.

The 21 mistakes page is a good start.

Thanks

Chris
 
The second example in the original post would be very useful if you are managing your page in a WYSIWYG editor.

If you are only using a text editor, you could get away with:

<? print &quot;<td>&quot; . $data[13] . &quot;\n</td>&quot;; ?>
or...
<? print (&quot;<td>$data[13]\n</td>&quot;); ?>

Regarding efficiency? @#$% it! If everyone wrote code efficiently, we would still be running on 64Kb RAM. Give those hardware manufacturers and operating system developers something to work for. [bigsmile]


- - picklefish - -
 
Another view:

I personally always use HTML templates with a template class. Here are the reasons why:

1. The PHP code is (99%) free of any concerns having to do with presentation.

2. The HTML can be edited with a WYSIWYG editor without the danger that editing interferes with the script logic. For example Macromedia Dreamweaver will keep the HTML in good shape and prevents syntax errors in the HTML.

3. PHP code can reside in a different place than the templates i.e., a designer can edit the templates in their own location without messing with the script.

4. Designs change. Just use a different template.

Having this strict separation between code and presentation has saved me an enormous amount of time.

If it is necessary to implement some kind of visual queues I use style attributed that can be set. The PHP provides a specific keyword to the HTML template that can be used as a class=&quot;&quot; attribute. The actual look can be altered later with the CSS stylesheet. No code change necessary.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top