Hi! I'm from London!
Thanks for sharing this information!

    20 days later
    laserlight;10856214 wrote:

    Unless there are objections or suggestions otherwise, I am making this a sticky.

    Note that style guidelines are always subjective. Feel free to use a different style, as long as you are consistent and keep to the spirit of what iceomnia suggested.

    I'd like to note that, on my system at least, when I'm posting code manually here and hit Tab it skips on to the submit button, or the next field in the form, so if you're posting a sizable chunk of code manually, perhaps do it in Notepad or another simple text editor beforehand and then paste it in.

      ellyka112;10968973 wrote:

      I'd like to note that, on my system at least, when I'm posting code manually here and hit Tab it skips on to the submit button, or the next field in the form, so if you're posting a sizable chunk of code manually, perhaps do it in Notepad or another simple text editor beforehand and then paste it in.

      Well then why not tell your editor to replace tabs with 4 spaces.

      You should be using 4 spaces anyway as this is the industry standard as tabs can be inerpreted differently in different programs.

        iceomnia wrote:

        Well then why not tell your editor to replace tabs with 4 spaces.

        That won't solve the problem since this jump of focus by a tab is a feature of the web browser, not the editor.

        iceomnia wrote:

        You should be using 4 spaces anyway as this is the industry standard as tabs can be inerpreted differently in different programs.

        I am not convinced that the use of 4 spaces, or even the use of spaces rather than actual tab characters for indentation, is "industry standard". Various settings related to indentation can be configured in many editors, so the problem is largely a one of consistency within the file, and perhaps within the project or team.

          laserlight;10969004 wrote:

          Originally Posted by iceomnia
          Well then why not tell your editor to replace tabs with 4 spaces.

          That won't solve the problem since this jump of focus by a tab is a feature of the web browser, not the editor.

          Ha ha - well I'm talking about when code is copied and pasted inot this website.

          If you are just tying into the box - use 4 spaces, or 8 or 16, or whatever - just be consistent.

          <?php
          
          class Indented
          {
              public function indentedFourSpaces($indented)
              {
                  if ($indented == true) {
                      return true;
                  }
              }
          }
          ?>
          

            Given the state of some of the code that gets posted here, I think I'd be happy with any kind of indentation so long as its more than none.

            For the record, I personally use tab characters to indent. Why? iceomnia said it best:

            iceomnia wrote:

            tabs can be inerpreted differently in different programs.

            Exactly! If I like tabs to be the width of 4 spaces, then I'll tell my editor that my tab width is four. If you like 2, then you can do likewise. The same data will then appear exactly as both of us prefer to see it.

            Why should I be stuck seeing your two spaces when I'd rather see four (and vice versa)? This is all not to mention the fact that a tab character is always 1 character, whereas using spaces means you've got a variable amount (probably more than 1) of characters all over the place for no other reason other than aesthetic whitespace.

              4 months later

              Something I can't believe I haven't seen mentioned is the way people format variables in strings. There are many options but nothing is more annoying to me than when someone is switching back and forth in a single script. The different formats I've seen are:

              // strings
              $string = "full";
              echo "The string is $string<br />";
              echo "The string is {$string}<br />";
              echo "The string is ".$string."<br />";
              echo 'The string is '.$string.'<br />';
              // will output 4 lines saying The string is full
              
              //arrays
              $arr = array("full");
              echo "The first array value is $arr[1]<br />";// will not work when using a string as a key
              echo "The first array value is {$arr[1]}<br />";
              echo "The first array value is ".$arr[1]."<br />";
              echo 'The first array value is '.$arr[1].'<br />';
              // will output 4 lines saying The first array value is full 

              Personally I have no problem reading any of these styles, however I do get really annoyed when someone refuses to stick to one method. Also note there are a few other syntaxes, but I can't think of them currently.

                Don't forget comma-separated arguments for echo. 🙂

                echo 'The string is ', $string, '<br />';
                
                Derokorian;10977583 wrote:

                Something I can't believe I haven't seen mentioned is the way people format variables in strings. There are many options but nothing is more annoying to me than when someone is switching back and forth in a single script. The different formats I've seen are:

                // strings
                $string = "full";
                echo "The string is $string<br />";
                echo "The string is {$string}<br />";
                echo "The string is ".$string."<br />";
                echo 'The string is '.$string.'<br />';
                // will output 4 lines saying The string is full
                
                //arrays
                $arr = array("full");
                echo "The first array value is $arr[1]<br />";// will not work when using a string as a key
                echo "The first array value is {$arr[1]}<br />";
                echo "The first array value is ".$arr[1]."<br />";
                echo 'The first array value is '.$arr[1].'<br />';
                // will output 4 lines saying The first array value is full 

                Personally I have no problem reading any of these styles, however I do get really annoyed when someone refuses to stick to one method. Also note there are a few other syntaxes, but I can't think of them currently.

                  Well I haven't seen that before when working with other's code. Of course I don't work with others code very often.

                    You will start to see more obscure formats now you've joined this forum.

                    Incidental, the commas in echo commands is said to be more efficient - but that's for another thread.

                      7 months later

                      Thats pretty nice and easy to understand a code as well as description is also very nice

                        3 months later

                        Going from the OPs example, I prefer coding like this:

                        // Correctly spacing a function with tabs to make it readable. 
                        function foo($bar) { 
                            // Use a coment to tell us what the block below is doing. 
                            if ($bar == true) { 
                                $something = true; 
                            } 
                        
                        return $somthing; 
                        }

                        If it was a single line, I would further condense this:

                            if ($bar == true) { $something = true; }

                        I hate wasting lines of code with single brackets where they are not needed.
                        It may seem like a small change but saves a lot of scrolling when you develop fully fledged systems.

                          Elev8;10996275 wrote:

                          I hate wasting lines of code with single brackets where they are not needed.

                          If a conditional (as well as loops) only has one statement it doesn't need braces at all.

                          if ($bar == true) $something = true;

                          I personally prefer to put it on the next line simply because it's a flow control structure and it makes the code look like it's literally flowing.

                            Bonesnap;10996294 wrote:

                            If a conditional (as well as loops) only has one statement it doesn't need braces at all.

                            if ($bar == true) $something = true;

                            I personally prefer to put it on the next line simply because it's a flow control structure and it makes the code look like it's literally flowing.

                            I was gonna say the exact same thing. Of course I still generally use two lines, with indentation:

                            if( $bar == TRUE ) 
                               $something = TRUE;
                            Elev8;10996275 wrote:

                            I hate wasting lines of code with single brackets where they are not needed.
                            It may seem like a small change but saves a lot of scrolling when you develop fully fledged systems.

                            You know what saves even more scrolling? Use a good editor that supports Code Folding

                              Derokorian;10996298 wrote:

                              I was gonna say the exact same thing. Of course I still generally use two lines, with indentation:

                              if( $bar == TRUE ) 
                                 $something = TRUE;

                              You know what saves even more scrolling? Use a good editor that supports Code Folding

                              Firstly, using braces allows for easy expansion at a later date without the need for trying to find find the end point.
                              It does work well in template code, similar to WordPress' layout...

                              <?php if($x=$y) : ?>
                              Random HTML
                              <?php else : ?>
                              Other HTML
                              <?php endif; ?>

                              Apart from that case, I find the atypical aspect of using it arbitrarily can cause confusion.

                              Secondly, code folding is just terrible IMO.
                              Both Dreamweaver and Notepad++ use "folding", but it's just such a bad habit to get into.

                              Maybe it's just a bad habit I have developed after being a notepad coder for 15 years.
                              Either way, I never have, and never will, use code folding.

                                I use code folding to collapse documentation out of the way, but I don't use it for pairs of braces. Instead I avoid letting the code between them get too long and hairy in the first place. If I find myself checking braces I take it as a sign that "here be long and hairy code" threatens and I work on sorting it out.

                                I have braces on their own line so that it takes less conscious effort to pair them visually, raising the threshold where I start deliberately checking. Using a 7-pt font further reduces the amount of eyeball roving involved.

                                  How is code folding a bad habit? When looking for a certain function to update I don't even have to scroll, I just expand that function only...

                                  Also since I tend to comment the way PHPDocumentor says to (which is supported for parsing by my editor) its super nice to get rid of those 40 lines of comments and get to just the code.

                                    Elev8;10996351 wrote:

                                    Firstly, using braces allows for easy expansion at a later date without the need for trying to find find the end point.

                                    Why would you need to "try and find" the end point? The end point is the very next line...

                                    Elev8;10996351 wrote:

                                    It does work well in template code, similar to WordPress' layout...

                                    <?php if($x=$y) : ?>
                                    Random HTML
                                    <?php else : ?>
                                    Other HTML
                                    <?php endif; ?>

                                    I can't stand the way WordPress does this in their template code. I usually end up modifying it to use braces, or remove it entirely and do my own thing.

                                    Elev8;10996351 wrote:

                                    Secondly, code folding is just terrible IMO.
                                    Both Dreamweaver and Notepad++ use "folding", but it's just such a bad habit to get into.

                                    How is it terrible and how is it a bad habit? 😕

                                    Weedpacket;10996354 wrote:

                                    I use code folding to collapse documentation out of the way, but I don't use it for pairs of braces. Instead I avoid letting the code between them get too long and hairy in the first place. If I find myself checking braces I take it as a sign that "here be long and hairy code" threatens and I work on sorting it out.

                                    I'll use it for functions, especially if I have a PHP file that has a bunch of functions in it. Most editors allow you to hover over the collapsed section and a small "tooltip" of sorts shows you the code that's collapsed, so you can at a glance see the function's code (at least part of it, anyway). I also never find myself "checking" braces since my editor highlights matching braces.

                                    Weedpacket;10996354 wrote:

                                    I have braces on their own line so that it takes less conscious effort to pair them visually, raising the threshold where I start deliberately checking.

                                    I also put braces on their own line; I feel it makes it not only easier to read, but it also gives literal meaning to "code block".

                                    Weedpacket;10996354 wrote:

                                    Using a 7-pt font further reduces the amount of eyeball roving involved.

                                    My eyes are bad so I code in size 14pt font. :eek:

                                    Derokorian;10996381 wrote:

                                    Also since I tend to comment the way PHPDocumentor says to (which is supported for parsing by my editor) its super nice to get rid of those 40 lines of comments and get to just the code.

                                    +1 for phpDocumentor comments.

                                      Using a 7-pt font further reduces the amount of eyeball roving involved.

                                      My eyes are bad so I code in size 14pt font.

                                      I have no idea what size font I use, I tend to use the ctrl+mouse scroll a lot to make it bigger or smaller. I tend to make it small to find what I'm looking for then bigger to change it...

                                        Bonesnap;10996583 wrote:

                                        Why would you need to "try and find" the end point? The end point is the very next line...

                                        I have never, ever used:

                                        if ($bar == true) $something = true; 

                                        without it expanding to include other code at a later stage.

                                        I thought that's what the condensed conditional statement was for?

                                        $var = (1 == 2) ? "A" : "B";

                                        I didn't say it was right or wrong, just my personal preference after many years of coding in notepad.
                                        I have severe OCD so like my code to be as small and readable as possible.
                                        I am also an autodidact, so my definition of readable probably isn't the same as yours.