An Oracle blog about NetBeans for PHP

Formatting - Blank Lines

Another formatting category that you can find in PHP formatting setting is Blank Lines category. If you don't know how to obtain settings for PHP formatter, look at the first blog from the formatting series. This category define adding and deleting blank lines between different elements in the PHP code.

As you can see on the picture, user can set how many blank lines the formatter should insert before or after an elements. Property defining blank lines around a namespace declaration and use statement are PHP 5.3 specific.

On the picture are displayed all properties that influence the inserting blank lines during formatting and also the default values.  

The algorithm deletes blank lines in groups of use statements and class fields declarations. The picture on the right sight displays an example with fields, where on the left side is code before formatting and on the right sight is the same code after formatting.

There are two groups of fields, the first one contains $field1, $field2, $field3 and $field4 properties. As you can see all the blank lines within the group of fields are removed, except the line before PHP doc comment for $field4 property, where is inserted the number of lines that is set to as Before Field property.

The second group of fields contains  $field5 and $field6 properties.

Similar behavior is for Use. So the properties Before Use, After Use, Before Field and After Field set to number of lines inserted before / after a group of use statements or fields, not the individual occurrences.

If there are two elements subsequently in the code that the first defines one blank line after the element and the second defines one blank line before the element, then there are not inserted two lines between these elements, but only one. Similarly only the bigger number of lines is placed between the elements, if the number of blank lines after the first element is not equal to the number of blank lines before the second element. 

See the picture below. The After Namespace property defines one blank lines after a namespace and Before Use property set to two lines. So two blank lines are created between the namespace declaration and group of use statements. 

From the picture above should be clear the meaning of properties Before Namespace, After Namespace, Before Use and After Use. The meaning of the rest properties is displayed on the picture below. For better understanding every property is set to one. The lines in the formatted code have color of the property that influence the number of blank lines. A few lines has two colors, because both property are counted for the line according the algorithm that is mentioned above. 

As you can see there are properties that define blank lines around a class (Before Class and After Class), blank lines after the open brace after class declaration (After Class Header) and blank lines before the close brace in class body (Before Class End).

The properties Before Field and After Field were already mentioned.

The last properties defines blank lines around a function / method (Before Function and After Function) and also blank lines before the close brace in function / method body (Before Function End).  

Join the discussion

Comments ( 25 )
  • Volker Dusch Thursday, February 11, 2010

    This looks really promising.

    More rules the team does not have to worry about because the IDE can do the job in place.

    Thats a big bonus point for netbeans in my book.

    Just love it, thanks a lot !

  • Pet Thursday, February 11, 2010

    Great! The more configurable NB is, the more we enjoy it

  • Tom Thursday, February 11, 2010

    I really appreciate your efforts and thanks again for a job well done.

    However I'm not sure if this is the best (easiest) way to do it. I somehow got the impression you are step-by-step re-implementing functionality to enforce coding styles. Don't get me wrong: I think this is great!

    But: wouldn't on-board scripting capability do the same thing, plus it could serve far more purposes? Or simply a button to execute some external program and add the current file path to it? If in addition there was an easy way to distribute and share these scripts and settings, this would be heaven!

    If we just had a chance to use our own scripts in NetBeans, we would write and share those ourselves. BTW: Checking a rule via regular expressions is a 3-liner in PHP and a lot less complicated than in Java.

    We are PHP-programmers - not Java-programmers. Just don't tell us that we need to write Java to get it up and running. Let us do it in PHP and we will be fine.

    I'm also worried about Oracle. So if we just had a really easy way to write our own small extensions using our scripting language of choice, we didn't have to worry so much anymore.

    I also think this would mean a big plus for Java developers as well.

  • Petr Pisl Thursday, February 11, 2010

    To Tom: There are few reasons why there has to be build in formatter.

    - formatting is not used only for formatting action.

    - running external script after every action which requires formatting / indentation (pressing enter, inserting an item from code completion window, templates) is too expensive. The editor in such case will be very slow.

    - you can not have full control over external process, that is started from java application

    - depending on a php script means that you can not use NetBeans on a machine where is not installed PHP runtime.

    - running out of the box

    - it's not possible to put result of parsing (AST) and lexical analysis of the code to a script. Such script has to do it again.

    - our parser is "hacked" that is able to somehow parse broken code (and in the editor in 90% you have broken code).

    But you are right, it's possible to do a plugin which will work as you describe. But still there has to be a formatter that will be used by NetBeans internally.

  • gaurav Thursday, February 11, 2010

    The screenshots described here has some extra features which are not available in the version that I am using.

    I am using netbeans v6.8 and I am unable to find out extra options for removing blank lines in the code. :-?

    please help.

  • Petr Pisl Thursday, February 11, 2010

    To gaurav: These features are not available in NB 6.8. They are available in the development builds, which can be downloaded from netbeans.org (nightly builds : http://bits.netbeans.org/download/trunk/nightly/latest/ ) or also you can download a continual build from our server: http://bertram.netbeans.org/hudson/job/PHP-build/

    The formatting setting is pretty new for PHP and I'm sure that it is not bugless. If you try it, please report every bug.

  • Bruce Ingalls Thursday, February 11, 2010

    This will be even nicer, if these properties can be shared with CodeSniffer:


  • Petr Pisl Thursday, February 11, 2010

    To Bruce: I was thinking about integration the code sniffer directly into NB, but time probably will not permit it. You are right, there is space to improve.

  • Tom Friday, February 12, 2010

    @Petr you might want to talk to the Java guys about CodeSniffer integration. It outputs Java-CheckStyle compatible XML files.

    Maybe the Java folks already implemented support for CheckStyle that could work just as well for CodeSniffer. Or in case they haven't: maybe they would like to see CheckStyle support in NetBeans as a new feature.

  • Tom Friday, February 12, 2010

    @Bruce you might already know that, but your plugin won't work on Windows (probably also won't work on Mac either). Also you can't select your own coding standard in the options dialog and have to use one of the preinstalled ones.

  • Pet Sunday, February 14, 2010

    If I copy-paste following code and select all and format, why does nb not reformat code so there are no blank lines as specified in format rules?

    class Application_Model_GuestbookMapper


    protected $_dbTable;

    public function setDbTable($dbTable)


    if (is_string($dbTable)) {

    $dbTable = new $dbTable();


    if (!$dbTable instanceof Zend_Db_Table_Abstract) {

    throw new Exception('Invalid table data gateway provided');


    $this->_dbTable = $dbTable;

    return $this;



  • Petr Pisl Monday, February 15, 2010

    To Pet: Which version of NB do you use? These formatting setting is new in NetBeans 6.9 development builds.

    Anyway the new lines are corrected only in places, which are defined by the properties. The blank lines in the code (like in if in your example), are not affected.

  • Bruce Ingalls Wednesday, February 17, 2010

    Nice. This will be even nicer, if you can synchronize your work with PHP CodeSniffer. Take a look at


  • robo47 Wednesday, February 17, 2010

    An Option for blank lines after comment-blocks would be nice, so instead of removing all blank lines here:


    \* some comment




    \* another comment



    and create


    \* some comment




    \* another comment



    it should be possible to create formatting like:


    \* some comment




    \* another comment



    So four additional Options like "After Commentblock" "Before Commentblock" and for single-line-comments "After Single Line Comment" and "Before Single Line Comment" would be nice

  • Bruce Ingalls Wednesday, February 17, 2010

    Sorry about the dual post (Browser cache issue). FWIW, comment preview does not show other comments.

    Note that CodeSniffer can read properties, such as ini-style files. This may be enough to contact the PEAR & Netbeans plugin author, to see if there is any interest in synergy.

    I'm not a spokesman for the Whitewashing.de plugin, but some fixes are arriving for issues that you raise.

    On one hand, RATS & YASCA


    work nicely for multiple languages. However, it may be possible to leverage an existing PMD plugin for PHPMD http://phpmd.org/ support; I think PHPMD currently only supports sniffing, anyway.

    HTH, Bruce

  • Petr Pisl Thursday, February 18, 2010

    To robo47: I can create new options for blank lines before and after php comment and php doc comment. I'm not sure, whether it would be useful for single line comment, because sometimes you can want to keep more line comments together and other separate.

    To Bruce: Thanks for the links. I think that the mentioned tools can be integrated, but it's slightly different from formatting.

  • Raazy Wednesday, June 23, 2010

    Code formatter is pretty good thing, and I very appreciated its presence in Netbeans.

    But due such improvements (which are damn good in general) it produces lots of false changes when your code is under source control, which was saved by NB 6.8.

    This leads to big trouble when you looks at diff after Reformat in 6.9 and trying to locate small piece of problematic code between huge changes.

    Just imagine, when you work with team and your team is working on few big projects which are already formated according 6.8 scheme. This new scheme 6.9 is practically denied for whole team, and has to be.

    Here are some suggestions, which might help with this issue:

    a) bring back the code formatter from 6.8 and let the user to decide, which version will use, eq. some checkbox, "Use old scheme?" which will actually "switch" the format engine.

    b) add "leave untouched" checkbox next to every option in formatter settings - especially in "Blank Lines" section, so it will be possible to setup new formatter to behave close to old one.

    Option A seems to be a quick and elegant solution, but B can bring way better possibilities on beautifying code.

    Also option for export formatter settings only will be great also.

    Thanks in advance,


  • Petr Pisl Thursday, June 24, 2010

    @Radek: "Old formatter" didn't exist. The formatter before was basically just indenter (functionality of the indenter was called). Yes, the new formatter have few problems. Please report them separately for the cases.

    The option A basically is nothing, because the Old scheme was not there. The option B is doable. Again the best solution would be if you can file a bug for it.

    Regarding changes in the source code. It's good practice don't format source code, when there is another change and formatting changes commit separately. So then it's clear that the change is "just" visual.

  • Raazy Thursday, June 24, 2010

    @Petr Pisl: Good point with separate commits; it sounds rather like an compromise, but it's possible.

    Can you tell me, where would be best to file a bug for this so called option B?



  • Petr Pisl Thursday, June 24, 2010

    @Radek: You can enter an issue from this page: http://netbeans.org/community/issues.html . There is also a link to a document, how to enter a bug etc. You have to create netbeans account, but it's easy:). Thanks.

  • Raazy Friday, June 25, 2010

    Sorry for spamming here but I am unable to file a bug.

    When I trying to open this http://netbeans.org/bugzilla/enter_bug.cgi

    to post a bug I always get 500 Internal Server Error

    Is your bugzilla OK?

  • Mike Tuesday, July 13, 2010


    I mostly like the PHP auto-formatting in Netbeans. Currently using (Build 201007040001).

    One issue is whitespace between end of code and a comment. I like tabs for readable comments that are lined up vertically and auto-format happily wrecks that and I don't see an option to stop it.

    $code<tab>= something;<tab><tab>// comment etc.

    $m< ><tab>= lilbit;< ><tab><tab>// more comments

    It seems to add spaces around operators and after semicolons which is okay, but most of my tabs are getting whacked also. I avoid using is on commented blocks but it is so great to do Ctrl-A then Shift-Alt-F I sometimes forget ;)

    Thank you for all your efforts


  • Petr Pisl Wednesday, July 14, 2010

    Hi Mike,

    could you file a new issue for this. I think it can be considered as a bug. The issue you can enter here http://netbeans.org/community/issues.html



  • Jens Tuesday, November 2, 2010

    in reply to Bruce post...

    I'm currently working on a NetBeans plugin, which displays phpMd (http://phpmd.org/) and phpCodeSniffer (http://pear.php.net/PHP_CodeSniffer) messages in the nb task area. At the moment it only works with nb 6.9.1, but it works ;)


  • buenon Monday, August 20, 2012


    I was wondering regarding robo47 post on blank lines after comments.

    I like using editor-folds. However I would have liked to have a blank line between them after i auto format.

    I'm guessing that a blank line after a comment will not be comfortable in other places so any chance for a blank line after editor-fold?

Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.