Jump to content

Coding Style


Stephen

Recommended Posts

  • Replies 76
  • Created
  • Last Reply

Top Posters In This Topic

Well I'll add my two cents

 

I like to call it "pretty but optimized"

<?php

if ( function( $parameter, $parameter ) ) {
print 'Hey ' . $var;
} else {
print ( 10 / 5 ) + ( 12 / 6 );
}

?>

 

Always print over echo, always single over double quotes, always spaces after parentheses.

Link to comment
Share on other sites

Well I'll add my two cents

 

I like to call it "pretty but optimized"

<?php

if ( function( $parameter, $parameter ) ) {
print 'Hey ' . $var;
} else {
print ( 10 / 5 ) + ( 12 / 6 );
}

?>

 

Always print over echo, always single over double quotes, always spaces after parentheses.

 

What's 'optimized' about it?

 

And why print over echo? Theoretically, echo should be faster than print (because print is a function, echo a construct), though no-one has been able to produce a conclusive benchmark to support it.

 

 

Link to comment
Share on other sites

I code exactly like the person I plagarised from - oops did that come out loud???

 

only joking by the way guys...

 

I use method #2 I prefer all my braces to be vertically coincident so I can easily see which opening and closing braces match( obviously on small functions / code ), I write using PHPDesigner which places the matching braces under the opening one and I find it much better for me to debug my own crap that way. ( usually takes ages so it has to be easily read)

Link to comment
Share on other sites

I think having braces on there own line make it easier to read code that has nesting control statements.

 

That's why I switched to that style myself.  I kept getting a lot of parse errors because I'd forget a brace or two.  Doing it like this:

function myFunc($arg)
{
   foreach($arg as $value)
   {
      echo $value;
   }
}

 

Makes it a lot easier on me, and saves me time in the long run as I don't have to hunt for matching braces.

Link to comment
Share on other sites

do while($_GLOBALS['owight']){
   do{
      if(check() == 1) echo"<input type=\"text\" name=\"item_name\" value=\"".$item->item_name."\">";
      else if(check() == 2)  echo "<script>get_adverts(\"main_content_area\");</script>";
      else if(check() == 3)  echo "<script>get_adverts('main_content_area');</script>";
      else foreach($things as $thing) echo $thing["thing_name"];
   }while($_SESSION["ok"]);

   while(mysql_fetch_array($results)) echo $results["feild"];
}

Link to comment
Share on other sites

"do while($_GLOBALS['owight']){"

 

Is that even a valid construct?

 

Why not just while?

 

 

 

"Makes it a lot easier on me, and saves me time in the long run as I don't have to hunt for matching braces."

 

 

Lots of editors highlight matching braces, but I can see what you're saying.  I personally like:

 

if() {

 

}

 

But, logically

 

if()

{

 

}

 

Makes more sense....

 

Link to comment
Share on other sites

Well I'll add my two cents

 

I like to call it "pretty but optimized"

<?php

if ( function( $parameter, $parameter ) ) {
print 'Hey ' . $var;
} else {
print ( 10 / 5 ) + ( 12 / 6 );
}

?>

 

Always print over echo, always single over double quotes, always spaces after parentheses.

 

What's 'optimized' about it?

 

And why print over echo? Theoretically, echo should be faster than print (because print is a function, echo a construct), though no-one has been able to produce a conclusive benchmark to support it.

 

 

 

1.print is not a function...http://www.php.net/print

2.Also, PHP was born from C, and C uses print...

3.Lastly, echo checks for parameters whereas print does not, so the return value vs. the check evens out timewise (roughly)

 

Single quotes parse faster than double quotes, you can get conclusive proof of that looking at the php source, this is also why i concatenate how I do.

 

Overall, I guess it's just my programming roots in C coming out in PHP.

Link to comment
Share on other sites

Makes it a lot easier on me, and saves me time in the long run as I don't have to hunt for matching braces.

 

Lots of editors highlight matching braces, but I can see what you're saying.  I personally like:

 

if() {

 

}

 

But, logically

 

if()

{

 

}

 

Makes more sense....

 

Oh, I know that a lot of editors highlight matching braces.  I mean, I use Notepad++, so I have it right at my fingertips.  But, it doesn't really help much if you have several long nested blocks which require you to scroll to see one of the highlighted pair.  I rely much more on the indentation markers myself, which is one of the reasons why I like a block style to my coding.

Link to comment
Share on other sites

Makes it a lot easier on me, and saves me time in the long run as I don't have to hunt for matching braces.

 

Lots of editors highlight matching braces, but I can see what you're saying.  I personally like:

 

if() {

 

}

 

But, logically

 

if()

{

 

}

 

Makes more sense....

 

Oh, I know that a lot of editors highlight matching braces.  I mean, I use Notepad++, so I have it right at my fingertips.  But, it doesn't really help much if you have several long nested blocks which require you to scroll to see one of the highlighted pair.  I rely much more on the indentation markers myself, which is one of the reasons why I like a block style to my coding.

 

Heh, yeah I use Notepad++ too.  Seems more logical to me to have same level curly braces, but habbits die hard x.x.

Link to comment
Share on other sites

1.print is not a function...http://www.php.net/print

2.Also, PHP was born from C, and C uses print...

3.Lastly, echo checks for parameters whereas print does not, so the return value vs. the check evens out timewise (roughly)

 

Ah, yeah, I meant it returns a value. My bad. I did some dirty benching myself, and found that passing substrings to echo as arguments is REALLY slow. Print vs echo was too close to call. Anyway, as you point out yourself, it doesn't make any real difference if you use echo or print, performance wise. Doesn't make your coding style 'optimized'.

 

Single quotes parse faster than double quotes, you can get conclusive proof of that looking at the php source, this is also why i concatenate how I do.

 

No conclusive proof of that. Also, unlike the results at phpbench.com, some crude tests I did two years ago show that using double quotes on basic strings is actually slightly faster than using single quotes. We're talking 0.0001ms, and on a different system it might turn out the other way around. Concatenation vs nesting (expanding) variables in double quotes strings came out about 0.007ms in favour of the former. I guess the moral is: it doesn't matter.

 

Overall, I guess it's just my programming roots in C coming out in PHP.

 

That's fine, but it's just preference, doesn't make your coding style 'optimized'.

Link to comment
Share on other sites

1.print is not a function...http://www.php.net/print

2.Also, PHP was born from C, and C uses print...

3.Lastly, echo checks for parameters whereas print does not, so the return value vs. the check evens out timewise (roughly)

 

Ah, yeah, I meant it returns a value. My bad. I did some dirty benching myself, and found that passing substrings to echo as arguments is REALLY slow. Print vs echo was too close to call. Anyway, as you point out yourself, it doesn't make any real difference if you use echo or print, performance wise. Doesn't make your coding style 'optimized'.

 

Single quotes parse faster than double quotes, you can get conclusive proof of that looking at the php source, this is also why i concatenate how I do.

 

No conclusive proof of that. Also, unlike the results at phpbench.com, some crude tests I did two years ago show that using double quotes on basic strings is actually slightly faster than using single quotes. We're talking 0.0001ms, and on a different system it might turn out the other way around. Concatenation vs nesting (expanding) variables in double quotes strings came out about 0.007ms in favour of the former. I guess the moral is: it doesn't matter.

 

Overall, I guess it's just my programming roots in C coming out in PHP.

 

That's fine, but it's just preference, doesn't make your coding style 'optimized'.

 

fair enough :)

Link to comment
Share on other sites

since i seem to be challenged in reading code i do it like this:

<?php
if ((!isset($_POST[something)))
     {
      this
     }
else
     {
      if ("doo"=="doo")
          {
           echo "DOO";
          }
      else
          {
           echo "wee";
          }
     }

this way i can kind of see if and where i am missing squggly brackets.

Link to comment
Share on other sites

since i seem to be challenged in reading code i do it like this:

<?php
if ((!isset($_POST[something)))
     {
      this
     }
else
     {
      if ("doo"=="doo")
          {
           echo "DOO";
          }
      else
          {
           echo "wee";
          }
     }

this way i can kind of see if and where i am missing squggly brackets.

 

The is the first time i have even seen code formatted like that.

Link to comment
Share on other sites

Well I'll add my two cents

 

I like to call it "pretty but optimized"

<?php

if ( function( $parameter, $parameter ) ) {
print 'Hey ' . $var;
} else {
print ( 10 / 5 ) + ( 12 / 6 );
}

?>

 

Always print over echo, always single over double quotes, always spaces after parentheses.

 

What's 'optimized' about it?

 

And why print over echo? Theoretically, echo should be faster than print (because print is a function, echo a construct), though no-one has been able to produce a conclusive benchmark to support it.

 

 

 

1.print is not a function...http://www.php.net/print

2.Also, PHP was born from C, and C uses print...

3.Lastly, echo checks for parameters whereas print does not, so the return value vs. the check evens out timewise (roughly)

 

Single quotes parse faster than double quotes, you can get conclusive proof of that looking at the php source, this is also why i concatenate how I do.

 

Overall, I guess it's just my programming roots in C coming out in PHP.

 

 

i keep it

 

if(){

}

 

and when i come to edit it i open it up

 

if()

{

}

Link to comment
Share on other sites

do while($_GLOBALS['owight']){
   do{
      if(check() == 1) echo"<input type=\"text\" name=\"item_name\" value=\"".$item->item_name."\">";
      else if(check() == 2)  echo "<script>get_adverts(\"main_content_area\");</script>";
      else if(check() == 3)  echo "<script>get_adverts('main_content_area');</script>";
      else foreach($things as $thing) echo $thing["thing_name"];
   }while($_SESSION["ok"]);

   while($a = mysql_fetch_array($results)) echo $a["feild"];
}

Link to comment
Share on other sites

  • 4 weeks later...

i tab...dont know about standards...

 

When working in a team environment, spaces. Save newlines as \n, not \r\n.

 

Standards vary between teams, and can be anything from tabs to spaces to the combination of the two. The only thing that matters is consistency.

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.