Jump to content

Archived

This topic is now archived and is closed to further replies.

rajmohan

How to write PHP CODING format?

Recommended Posts

Can any one help me how to write the coding format?

And how to use functions and classes

Share this post


Link to post
Share on other sites
what do you mean by "coding format?" be more specific. format of what? some pre-defined function? php syntax rules in general? good coding practice? be more specific.

Share this post


Link to post
Share on other sites
i am asking about procedure the way of expression. Example if my coding is wided by another programmer how to they understand?

Share this post


Link to post
Share on other sites
are you asking what is the "best" way to do things like indent and put comments in your code, so that other people can better understand it?

Share this post


Link to post
Share on other sites
yes man you are correct. because the coding what we are doing should be understand by others.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
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)

Share this post


Link to post
Share on other sites
Thank you my friends this will be very usefull way to follow coding structure.

and if i am attending an interview they ask me to write a simple coding how can i express my coding to them.

Share this post


Link to post
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]

Share this post


Link to post
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

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

×

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.