Jump to content

How to write PHP CODING format?


rajmohan

Recommended Posts

well.. i pretty much answered your question in my question.  make sure to properly indent, and keep it consistent.  there are different indentation styles out there. there is no "right" way to indent. the key is to actually do it. and to keep it consistent.  for example, don't do this:

[code]if (blah) {
  blahblah
} else {
  blahblahblah
}[/code]

and then later on switch to this:

[code]if (blah)
{
  blahblah
}
else
{
  blahblahblah
}[/code]


and put comments everywhere.  the more the merrier.  also, make an effort to research and see if there is a better way to do something. the smaller the code, the better. and prettier. 

you could do this:
[code]
echo "1<br>";
echo "2<br>";
echo "3<br>";
echo "4<br>";
echo "5<br>";
[/code]
or you can do this:
[code]
for ($x = 1; $x < 6; $x++) {
  echo "$x<br>";
}
[/code]
which do you think is better?

Link to comment
Share on other sites

Personally, i use the following structure for language constructs:
[code=php:0]
if (condition) // space between if and (condition)
{ // on their own lines
    // 4 spaces, or 1 tab at 4 space widths
    echo 'hi';
}
[/code]

As for comments, use them. lots of them. and look at PHPDocumenter for generating HTML pages based on formated comments (http://www.phpdoc.org/)

For comments, it is most important to explain WHY you are doing something, then simply stating what you are doing.

Also, i never exclude curly brackets. even when i can. i find my code is a bit more readable that way
[code=php:0]
if(condition)
    print('hi');
// i would do:
if (condition)
{
    print('hi');
}
[/code]

mind you, these are only my personal preferances. do what looks best to you. But remember: Use the SAME coding-stlye, dont change halfway throgh. (this can lead to verry messy code)
Link to comment
Share on other sites

i personally go for putting the { on the same line,  indenting 3 spaces for the content, and lining up the } with the beginning. to me, it looks cleaner. but that's just me. As far as comments, well, look at example (try to look past the logic of the code :) ):

[code]
<?php
// if user clicked the submit button from form1
if ($_POST['submit']) {
  // did user enter in his name?
  if ($_POST['name'] != '') {
      //do something
  } else { // since user did not enter his name in..
      // add some more info to the form error message
      $error .= " no name entered.";
  } //end else no name
} // end if user clicked submit
?>
[/code]
Link to comment
Share on other sites

you are correct but if they give me a big module how can i express them? if i give more commands that will be disturb to view all lines.

putting commant line will be help full for if any one coming to modify the coding. but how to express the interviewer
Link to comment
Share on other sites

grab the phpbb source code, and in there is a text file that is like coding standards or something...it is basically an example on how they want the code to look if your gonna write mods.  I use that as a typical coding standard for all my code. PHPBB sucks as a forum, but their code is normally clean and such.  Double spaced in the approiate places, well commented, and indents.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.