Jump to content


Photo

How to write PHP CODING format?


  • Please log in to reply
11 replies to this topic

#1 rajmohan

rajmohan
  • Members
  • PipPipPip
  • Advanced Member
  • 169 posts

Posted 28 August 2006 - 05:42 AM

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

And how to use functions and classes

#2 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 28 August 2006 - 05:56 AM

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.


Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#3 Caesar

Caesar
  • Members
  • PipPipPip
  • Advanced Member
  • 1,025 posts

Posted 28 August 2006 - 05:59 AM

http://www.php.net

good source for all your questions. :-)
PHP Ninja

#4 rajmohan

rajmohan
  • Members
  • PipPipPip
  • Advanced Member
  • 169 posts

Posted 28 August 2006 - 06:11 AM

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

#5 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 28 August 2006 - 06:18 AM

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?
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#6 rajmohan

rajmohan
  • Members
  • PipPipPip
  • Advanced Member
  • 169 posts

Posted 28 August 2006 - 06:20 AM

yes man you are correct. because the coding what we are doing should be understand by others.

#7 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 28 August 2006 - 06:29 AM

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:

if (blah) {
  blahblah
} else {
  blahblahblah
}

and then later on switch to this:

if (blah)
{
   blahblah
}
else 
{
   blahblahblah
}


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:
echo "1<br>";
echo "2<br>";
echo "3<br>";
echo "4<br>";
echo "5<br>";
or you can do this:
for ($x = 1; $x < 6; $x++) {
   echo "$x<br>";
}
which do you think is better?


Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#8 Joe Haley

Joe Haley
  • Members
  • PipPipPip
  • Advanced Member
  • 103 posts
  • LocationCanada, eh?

Posted 28 August 2006 - 06:35 AM

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

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
if(condition)
    print('hi');
// i would do:
if (condition)
{
    print('hi');
}

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)
Give a man a fish; you have fed him for today.  Teach a man to fish; and you have fed him for a lifetime
Don't teach men to program. Teach them to fish.

Please, try the RTFM solution before asking for help:
http://php.net/manual/en/index.php

#9 rajmohan

rajmohan
  • Members
  • PipPipPip
  • Advanced Member
  • 169 posts

Posted 28 August 2006 - 06:45 AM

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.

#10 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 28 August 2006 - 06:45 AM

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

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

Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#11 rajmohan

rajmohan
  • Members
  • PipPipPip
  • Advanced Member
  • 169 posts

Posted 28 August 2006 - 06:53 AM

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

#12 steelmanronald06

steelmanronald06
  • Staff Alumni
  • Advanced Member
  • 2,004 posts
  • LocationOk

Posted 28 August 2006 - 12:40 PM

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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users