An Oracle blog about NetBeans for PHP

Formatting - Tabs and Indents

Few weeks ago I wrote that I try to improve indentation engine and formatting for PHP files. Some improvements in the indentation engine were done during fixing NetBeans 6.8 and with this post I would like to start a discussion about formatting, which I try to improve during these days. In the development builds there are new categories for the PHP formatting options. Now are available categories for setting Tabs and Indents, Braces, Spaces and  Blank Lines.

User can modify behavior of the formatter in two levels. In Editor category -> Formatting tab in Options dialog (Tools -> Options) you can modify setting for PHP formatting. You should select PHP in the Language combo box, if you want to change PHP specific settings.

These global options influence the behavior for all PHP files, except the files belonging projects that use their own setting. So the PHP formatting can be also influence on the project level. In Project Properties dialog is Formatting category and you can decide, whether the project will use global options or the project specific options. Also you can easily reuse options from other project after click on Load from other project button. Like in the global options you should select PHP in the Language combo box, if you want to change behavior of the PHP formatting for the selected project. 

The first category is Tabs And Indents. The options Number of Spaces per Indent, Tab Size and Right Margin are common for all editors and by default the global setting is used. The check box Expand Tabs to Spaces is clear. By default it is checked and it means that the existing chars '\\t' are replaced by the count of spaces that is defined as Tab Size property. Property Number of Spaces per Indent defines the maximum number of spaces that will be created after pressing TAB key to indent the source code.  When the check box Expand Tabs to Spaces is unchecked, then the defined number of spaces in Tab Size property is converted to the '\\t' char. For this reason is good if you set Number of Spaces per Tab to an even multiple or divisor
of Tab Size .  

The PHP specific options:

  • Initial Indentation -  In files only with PHP code it defines number of spaces, that are placed at the beginning of every line (except the PHP delimiters <?php, <? and ?>). In files, where is HTML mixed with php, the Initial Indentation defines number of spaces from beginning of the PHP delimiter. 
  • Continuation Indentation Size - If there is a long expression, which is on more lines, then the continuation of the expression is indented through this property.  
  • Items in Array Declaration - It's similar to previous property, but for an array declaration.

It should be clear from this picture:

The next time I'm going to write about next category. Try the latest development build and suggest what else should be configurable. 

Join the discussion

Comments ( 34 )
  • &Aacute;lvaro G. Vicario Wednesday, February 3, 2010

    It's nice to see more formatting options specific to PHP and I see the indentation ones are extremely fine grained. Perhaps the "Continuation Indentation Size" is not widely use: I guess people use the same size as in other lines or start the line in a relative fashion (to the right of the parenthesis or the right of the equal sign). In any case, it looks good and I look forward to more.

    However, my humble impression is that NetBeans needs a general improvement in basic editing capabilities. It's curious that a first-class IDE that already reached version 6 lacks features that most freeware editors offer, such as rectangular selections or a simple button to show/hide hidden chars (tabs, spaces and carriage returns), not to mention other features like smart vertical positioning\*. I believe PHP coders could benefit from such ground improvements.

    (\*) When I have this:

    $foo[] = '';

    $foo[] = '';

    ... type:

    $foo[] = 'abcdef';

    $foo[] = '';

    ... andI hit the "Down" key, I'd expect the caret between the quotes, not after the semicolon.

  • Urkman Wednesday, February 3, 2010


    it would be nice to have an option to add spaces inside braces, like this:

    For a function:

    foo( param1, param2 );

    or for an array

    $array[ 'foo' ];

    Thanks and greetings,


  • Patrick Webster Wednesday, February 3, 2010

    I would like to second Álvaro's feature request for rectangular selection. I know that Sandip Chitale created the Rectangular Edit Tools module that performed this in earlier versions of NetBeans, but he no longer works at Sun and his plugin page is dead. Does anyone know if his plugin is still available and if it works with NetBeans 6.8?

  • John Bailey Wednesday, February 3, 2010

    I second those basic requirements raised by Álvaro G. Vicario - IMHO block (rectangular) selection is a prerequiste of a modern editor. Its a very good environment otherwise.

    Keep up the good work

  • Xantiva Wednesday, February 3, 2010

    These are very good news. The latest dev build is nearly a week old: "201001280200". Does it contain this improvements?

  • Espen Wednesday, February 3, 2010

    Good to see you're working on things like this too, and not just "the big stuff". I've really missed the continuation indentation setting.

    In other news, I must respectfully disagree with Alvaro's suggestion. I can't remember the last time I needed rectangular selections (if they are what I think they are), and would not like to end up between the quotes when hitting "Down" in his example.

  • dalin Wednesday, February 3, 2010

    The naming of the indentation options is really confusing and inconsistent. Here's my suggestion:

    Initial Indentation

    Continuation Indentation

    Array Declaration Indentation

    Also I'm not sure that "Initial Indentation" is even necessary. Will anyone actually set this to something other than 0?

    I second Xantiva's disagreement with Alvaro.

  • wegus Thursday, February 4, 2010

    Indentation is a good point! Indentation is done by TAB! And redone? Right, there is no Keyboard-Shortcut per Default on that. I always have to define my SHIFT+TAB to move a block one TAB out manually. That's an anoying simplicity. PLEASE set SHIFT+TAB for Shift Block right, as there is already TAB to shift them on TAB to the left predefined in Netbeans. It's a proven Combination for moving selected Blocks within Indentations and i would apreciate both Shortcuts to be active by default! Thank you!

  • anton Thursday, February 4, 2010

    One good idea would be to support

    the Zend_Framework coding style

    (which I suppose is nearly the same than PEAR coding style)


    If you have this you have a good base,

    and most people would be happy.

    I personally would like

    to be able to transform:



    $a = 1;

  • Tomasz Budzynski Thursday, February 4, 2010

    How about trim white spaces at the end of line on save? I found this in Zend Studio with version 5.0 or earlier. When there are whitespace after function or other stuff, it will be trim after save or after source format function.

    Second feature is line/word warp. When line of code (PHP or HTML) is longer then screen width line will break to line bellow, but editor will see it as one line of code.

    I think C++ has great format code configuration for example. It will be awesome to have configration like that

  • &Aacute;lvaro G. Vicario Thursday, February 4, 2010

    I've only mentioned some basic editing features most simple editors have. It's sad to use a full fledged IDE and need to keep a text editor handy to fix bad indent or copy columns around. What's the exact problem with having features \*you\* don't need?

    If you care about performance, well, NetBeans is already slow, but we should blame code completion, syntax checking or Java. If you care about losing simplicity, most of these features are invisible unless you tweak in the Settings dialogues. For the same reasoning, they should get rid of all current formatting options and revert to Notepad capabilities.

    Perhaps I'm missing the point of your critics but IMHO NetBeans is a great IDE and a poor editor.

  • guest Thursday, February 4, 2010


    there is an issue for rectangular selection: https://netbeans.org/bugzilla/show_bug.cgi?id=13857 . Please add a comment there, it's the best way how to convince guys responsible for editor basic features to implement it. The same can be applied for other mentioned features that are missing. I can influence them personally, but still the issues from real users has more power:). Anyway it's good for me to know, what you want.

    To Alvaro:

    In your case with

    $foo[] = 'abcdef';

    $foo[] = '';

    Basically I can handle this in the PHP editor and I have created new issue for this as a reminder: https://netbeans.org/bugzilla/show_bug.cgi?id=180275

    To Urkman:

    The functionality for adding spaces for functions and arrays as you mentioned is not there yet, but I have already started to implement the Spaces category and it belongs here. I expect, that it will be available shortly after Milestone 1.

    To Patrick Webster:

    The plugin is still available here:

    http://plugins.netbeans.org/PluginPortal/faces/PluginDetailPage.jsp?pluginid=1174 , but I don't know, whether works. I would suspect that it doesn't. Anyway the feature is on the plan.

    To Xantiva:

    It offers me the development build 201002040200. What OS do you use? You can also use continual builds available at bertram: http://bertram.netbeans.org/hudson/job/PHP-build/

    To Dalin:

    Thanks for the suggestion. I correct it and commit the change today.

    To wegus:

    I will try to discuss your suggestion, but again the issue in our database has sometimes more power than I:).

    To Anton:

    My plan is that I will add a category, where user can select predefined style. But at first I have to implement all the setting to configure the style. Thanks for the link.

    The case with $a=1 belongs to the spaces category, which is already there and this case I will add soon.

    To Tomasz Budzynsk:

    The trimming spaces functionality is there and also it was discussed here: http://blogs.sun.com/netbeansphp/entry/removing_trailing_spaces



  • Xantiva Thursday, February 4, 2010

    @dalin: You want to second the disagreemnt from Espen ...

    @Petr: Now I can see also the build 201002040200, maybe a cache problem. Thanks.

    To the spacing:

    It would be also very helpful, if a transformation like this would be possible ..

    $this -> DoSomething();





  • thinsoldier Thursday, February 4, 2010

    @Álvaro G. Vicario

    I've said it before and I'll say it again. IDE's are great at being IDE's but they all suck at being "text editors"

  • thinsoldier Thursday, February 4, 2010

    @Álvaro G. Vicario

    "... andI hit the "Down" key, I'd expect the caret between the quotes, not after the semicolon."

    In general I disagree with you but I do agree with that particular case. The problem is they'll have to identify and create instructions for many individual cases when you'd want that to happen.

    Personally I'm o.k. with the default behaviour.

    if I have the caret on character 17 of a 19 character line I expect the down arrow to take me as close to 17 as it can on the next line (which is after the semi-colon). I can live with that.

    But for those special cases like this what I'd like to be able to do is make a rectangular selection right through the middle of the quotes

    $foo[] = '|';

    $foo[] = '|';

    $foo[] = '|';

    $foo[] = '|';

    | = highlighted area of rectangular selection

    And then make the editor go into a mode where every time I press return it takes me to in-between the quotes on the next line. That's a general purpose way of doing this that works with a lot of cases with one technique.

    Even better would be if I could ctrl+alt+shift click in multiple spots in a file and have the return key take me to each one in succession. This way I can quickly enter repetitive things even though the structure of the text isn't in a predictable pattern.

    Or even when it is but not on multiple lines:

    $foo = array('|','|','|','|','|');

    $xRay = new MyClass($foo[|], |, '|');

    Soft wrap is more important than any of this.

  • Martin Vrkljan Thursday, February 4, 2010

    Shift+Alt+F breaks slightly when passing an array as an argument to a function. Here's an example ( __ => indentation ):

    Before Shift+Alt+F:

    foobarFunction (

    __array (

    ____'someArray' => array (

    ______'foo' => 'bar'




    After Shift+Alt+F:

    foobarFunction (

    __array (

    __'someArray' => array (

    ____'foo' => 'bar'




    The first indentation within the passed array seems to dissapear.

  • Martijn Friday, February 5, 2010

    These are great improvements. I love to see more of this stuff. This way it becomes a lot easier for us to let NetBeans use our coding standard. One thing I'm still missing is the position of the braces. Default is:

    function _some_function() {



    I would love to see an option to change the default to:

    function _some_function()




  • Petr Pisl Friday, February 5, 2010

    To Martin Vrkljan:

    I have created new issue for this:


    To Matinjn:

    The possibility to place { on the new line is already there. You can find it in Braces category.

  • alex Friday, February 5, 2010

    Hi Petr. Is there any information about the fate of NetBeans-Php, will it be developed by you guys ;) or the burden of Nb development will be shifted onto community? Sorry for offtopic, but it really bothers me...

    Btw, already downloading the devbuild, will leave feedback later.

  • Xantiva Friday, February 5, 2010

    There is also a problem with the indentation of comments: (Build 201002050200)

    $defaultStreamOptions = array(

    ____'http' => array(

    ________'protocol_version' => '1.1',


    ____'ssl' => array(

    ____// comment

    ________'verify_peer' => true,



    $a = array(

    ________// comment

    ____'foo' => 'bar',

    ____'foo2' => 'bar2',


    They should be oriented on the indentation of the line below. Both examples are wrong formatted.



  • Xantiva Sunday, February 7, 2010

    ??? Where is the last post about "Formatting Braces"?


  • Petr Pisl Sunday, February 7, 2010

    Probably I have just edited the post. I haven't delete it and now is clear shy I edited it. :)

  • Xantiva Tuesday, February 16, 2010

    Hi Petr,

    even if you are doing a very well job for the spaces, ... I have to come back to the indentations!

    I installed the 6.9 milestone 1 and tried to format this:

    $a = array(

    // comment

    'foo' => 'bar',

    'foo2' => 'bar2',


    After formatting the comment is NOT indented, the 'foo' ... is indented 4 spaces.

    Then enter one space before the comment and format again: The comment is indented 8 spaces. :(

    Is this a bug, or a feature? ;)

  • Petr Pisl Wednesday, February 17, 2010

    To Xantiva: The described behavior is designed with this way, but there is a bug. It was designed: if the line comments starts at 1 column on a line, then the comment will not be indented, in other case will be indented. So there is a bug, because it's indented in wrong way, if it doesn't start on the first column. The same behavior is designed for php comment and doc comment, where should be compared the first line of the comment.

    I have to admit that I haven't touched the comment formatting yet. I have to look at this and write it properly. Probably introduce new option that will influence the behavior.

    If you want, you can enter a new bug into our database.

  • robo47 Wednesday, February 17, 2010

    Would it be possible to make the Continuation Indentation Size 'dynamic' on for example assignments based on the above line for making code more readable like here:

    1) ____public function foo() {

    2) ________$somevarnam = 'asdfasdfasdf' .

    3) ______________________'asdfasdfasdf';

    4) ____}

    the third line here would need some kind of "dynamic" indention based on where the = stands in the line above.

  • Petr Pisl Thursday, February 18, 2010

    To robo47: i would like to introduce one more category "Alignment", which could handle this.

  • Oren Wednesday, April 7, 2010

    I would like to see "indent guide" lines for each "indent column" as in notepad++, examples (i can't seem to post regular urls):

    link: www.notepad-plus.sourceforge.net/commun/screenshots/scrsh_VibrantInk.png"

    link: www.notepad-plus.sourceforge.net/commun/screenshots/scrsh_lexerJava.gif

    I found it very helpful.


  • jmcbade Tuesday, May 4, 2010

    What happened to the right click menu as it no longer seems to include the "Run" and "Debug" menu items?

    Running M1 on a MAC


  • Pawe&#322; Chuchma&#322;a Wednesday, June 2, 2010

    It would be nice to format arrays like this:

    private $letters = array("A",




  • Yuriy Gerasimov Sunday, December 5, 2010

    What would be alse very very handy is to format equal statements as following:

    if we have

    $abc = 'hello';

    $a = 'hello';

    $bc = 'hello';

    after magic command to have

    $abc_= 'hello';

    $a___= 'hello';

    $bc__= 'hello';

    Same goes for assiging keyed array:

    $a = array(

    'a' => 'hello',

    'bcd' => 'hello',

    'cd' => 'hello',


    to get:

    $a = array(

    __'a'___=> 'hello',

    __'bcd'_=> 'hello',

    __'cd'__=> 'hello',


  • search engine optimization melbourne Monday, December 6, 2010

    Very nice, If there is N spaces in a row (where N is tab size) when invoking reformat, they are replace by one tab character. Tab key does not insert one tab char but tyies to add as many tab as it can, a the rest of space to the value of indentation level is filled with spaces. The spaces are never replaced by tab char when typing them manually.

    but when i hit Format, all tabs are convert into spaces

    if uncheck the "Expand tab to spaces" and Format the code

    the space before the x,y,width,height are removed

    which make the x,y,width,height no align with the bracket


  • Seo Company Tuesday, February 22, 2011

    I Read It's blog comments and Learn Php code. Good Help Received me. It's comments write what is php code? Follow me....

    PHP is a server-side scripting language.

    What You Should Already Know

    Before you continue you should have a basic understanding of the following:


    \* JavaScript

    If you want to study these subjects first, find the tutorials on our Home page.

    What is PHP?

    \* PHP stands for PHP: Hypertext Preprocessor

    \* PHP is a server-side scripting language, like ASP

    \* PHP scripts are executed on the server

    \* PHP supports many databases (MySQL, Informix, Oracle, Sybase, Solid, PostgreSQL, Generic ODBC, etc.)

    \* PHP is an open source software

    \* PHP is free to download and use

    What is a PHP File?

    \* PHP files can contain text, HTML tags and scripts

    \* PHP files are returned to the browser as plain HTML

    \* PHP files have a file extension of ".php", ".php3", or ".phtml"

    What is MySQL?

    \* MySQL is a database server

    \* MySQL is ideal for both small and large applications

    \* MySQL supports standard SQL

    \* MySQL compiles on a number of platforms

    \* MySQL is free to download and use

    PHP + MySQL

    \* PHP combined with MySQL are cross-platform (you can develop in Windows and serve on a Unix platform)

  • Seo Company India Tuesday, September 20, 2011

    Tab key does not insert one tab char but tyies to add as many tab as it can, a the rest of space to the value of indentation level is filled with spaces. The spaces are never replaced by tab char when typing them manually.

  • Emmet O'Grady Sunday, August 25, 2013

    How about an option in addition to the "Always", "If Long" and "Never" options which would "Leave current indentation alone".

    For example I like to put each command of a ternary expression on one line:

    $string = ($number > 5)

    ? 'Number is greater than 5'

    : 'Number is less than 5';

    Unless the ternary is *really* short, when I prefer putting it on one line:

    $string = ($number) ? 'Yes' : 'No';

    A nice feature would be an option to tell Netbeans 'Don't autoformat this specific case, let me handle it'.

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