Is there an equivilent of c#'s #region in PHP?
-
This is a question about a feature of an IDE (this is a visual studio feature, not a C# feature), and is off-topic here. – NDM Aug 08 '14 at 09:21
-
2Could always break out that stuff into another class or module. The more I look at my code and the regions the more I feel like I have classes hding in there. Now back to making more regions in my code – vikingben Feb 26 '18 at 20:37
-
Kitek's answer describes IDE support for `
`. Darin Peterson's answer describes phpstorm's more recent support for `#region`. – ToolmakerSteve Apr 22 '19 at 12:51
17 Answers
No, there's nothing directly in the language.
But every decent editors allow some kind of markup to allow this.
For example in Netbeans :
// <editor-fold defaultstate="collapsed" desc="user-description">
...any code...
// </editor-fold>
This syntax also works in all the Intellij IDEA editor family, see http://blog.jetbrains.com/webide/2012/03/new-in-4-0-custom-code-folding-regions/
You can also emulate the feature in Eclipse via a plugin : Code folding plugin for Eclipse?
Visual Studio Code includes it since version 1.19 :
#region user description
...any code...
#endregion
-
8You can write `fcom` and press tab to insert code template with editor-fold. – Kamil Nov 09 '12 at 22:39
-
You can read about it here: http://blog.jetbrains.com/webide/2012/03/new-in-4-0-custom-code-folding-regions/ – Piotr Czyż Mar 19 '14 at 13:13
-
1InteliJ also supports this in their IDEs (PHPStorm, idea ..) along with "//region Description //endregion" You can wrap any selection with ctrl+shift+Y – Aiphee Mar 13 '15 at 14:31
-
@Iscarneiro Not really, since the correct answer is that _PHP_ doesn't have it. This answer, while very useful, just gives examples for a couple of IDEs. – SQB Jul 15 '16 at 13:30
-
-
Thank you. As a professional c# programmer, and an amateur php programmer, this is perfect! :-) – Simon Parker Apr 11 '21 at 00:03
No.
The thing is, C# is sort of designed to be written by only one IDE, because Microsoft need you to always use their tools. So, built into the language (sort of) are things that affect the IDE.
PHP, on the other hand, is just a language. It's not supposed to have a symbiotic relationship with some specific editor, so it doesn't. So, no, it has nothing to control your editor.
Still, all proper programming text editors support folding class definitions, function definitions, and most scope blocks. Some editors may allow certain extensions to go beyond this.

- 378,754
- 76
- 643
- 1,055
-
18You can write in C# any text editor like in any other language. You don't have to use Visual Studio, anyway writing in notepad is very inefficient. – Kamil Nov 09 '12 at 22:40
-
2@Kamil: Sure, but `#Region` doesn't do anything in "any text editor". Then again, it still has its semantic meaning. – Lightness Races in Orbit Nov 10 '12 at 01:13
-
28If PHP had #region, all php editors could have taken advantage of it to make development more easier. #region is really great because, at any given time you can focus on things you directly working on, and hide all other parts. – synergetic Dec 08 '12 at 06:09
-
1I'm actually not a fan. I don't like hiding things, and I have no problem getting distracted by code in other files or code three pages down. It's just an additional mouse click when I _do_ want to look at that code. What I _do_ like about it is the self-documenting nature of grouping related functions and stuff. – Lightness Races in Orbit Dec 08 '12 at 13:03
-
3Well, your answer is true, but what PHP *can* do, is to ignore `#region` code lines, and let IDEs implement (or not) code folding on them. – Eduard Luca Apr 14 '14 at 12:52
-
1@EduardLuca: FWIW, I'm more a fan of this now than I was three years ago. :) – Lightness Races in Orbit Apr 14 '14 at 14:01
-
1Interestingly enough, Visual Studio for sure recognizes and folds `#region` in .php files. 2015, that is. – Nov 27 '16 at 14:25
-
3
-
`#whatever` is a preprocessor directive. It's good to have them. Why don't the PHP IDEs "JUST DO IT" and support some of them? Notepad++ and PHPStorm do support them though. That's all I need. – Bitterblue May 25 '18 at 06:29
-
@Bitterblue: _"`#whatever` is a preprocessor directive."_ But it isn't. – Lightness Races in Orbit May 25 '18 at 09:48
-
-
@Bitterblue: Okay. Which version of PHP introduced a preprocessor? – Lightness Races in Orbit Jun 12 '18 at 12:39
-
@LightnessRacesinOrbit I didn't say anything about them being introduced in PHP. I was talking about C, C++, C# and most probably other languages. C is the mother of modern langauges. So every experienced programmer should recognize `#whatever` as a preprocessor directive. – Bitterblue Jun 13 '18 at 14:00
-
1@Bitterblue: We are talking about PHP here. The comment of yours that I was replying to was all about PHP too. In PHP, `#whatever` is not a preprocessor directive; it cannot be, because PHP has neither a preprocessor nor a preprocessor language. – Lightness Races in Orbit Jun 13 '18 at 16:00
-
1
-
The only necessary part of this answer is the first word. (The rest is speculative opinion.) FWIW, preprocessor directives *are* part of C# - see section `9.5 Preprocessor directives` of [Ecma-334](https://www.ecma-international.org/publications/files/ECMA-ST-ARCH/ECMA-334%201st%20edition%20December%202001.pdf) spec from 2001. Its useful to distinguish between *any text editor* and *a full-featured code editor*. Of course its easy to see it this way in hindsight, now that .Net core is open source, as well as Rosalyn compiler, and there is widespread IDE support for C# :) – ToolmakerSteve Apr 22 '19 at 12:54
-
-
1Ok. I am trying to serve the goal of SO as a repository of technical knowledge, not a blog or opinion forum. First word and last paragraph fit that goal. Inbetween is a distraction, IMHO. PHP sentence could be reworded to neutral POV. C# sentence is factually incorrect, very non-neutral, and not salvagable, as far as I can see. – ToolmakerSteve Apr 22 '19 at 15:14
-
@ToolmakerSteve I'm sorry you feel that way. Feel free to write a competing answer. – Lightness Races in Orbit Apr 23 '19 at 10:31
-
2This answer is so biased and incorrect. # is a comment in PHP. Whatever follows the # is of absolutely no concern to the interpreter. krtek's answer should be the accepted one though it could include that in C# the # is a preprocessor directive, so while the inner workings are different, the outcome is the same in case of `#region` if the editor supports it. – Heki Jun 17 '20 at 08:25
As everybody has said before, the #region in .Net is actually a Visual Studio feature, not a C# one, but the fact that the C# grammar supports the syntax, allows its usage by any IDE that wants to implement it. Since in PHP, the '#' character is also used for comments, the same can be done by the IDEs. In fact, JetBrains' PhpStorm does it: https://blog.jetbrains.com/phpstorm/2012/03/new-in-4-0-custom-code-folding-regions/

- 737
- 6
- 8

- 1,447
- 21
- 22
-
3Just to ppl reading this later: #region works fine with php together with notepad++, folding and everything. – Benjamin Karlog Dec 15 '15 at 07:25
-
Both _//region Description_ and _#region Description_ works in PhpStorm – NicklasF Aug 16 '19 at 09:39
There is no equivalent (the other answers explain why), but let me show you how I do it:
//////////////////////////
/* REGION A */ {
function SomeFunction() {
return true;
}
function AnotherFunction() {
return false;
}
}
//////////////////////////
/* REGION B */ {
function ThirdFunction() {
return true;
}
function FourthFunction() {
return false;
}
}
The curly braces allow me to fold the block of code (your editor will need to support cold folding, but almost all of them do), while I still see the region name and have a simple visual divider.
Folded Result:
//////////////////////////
/* REGION A */ {
//////////////////////////
/* REGION B */ {

- 609
- 1
- 7
- 14
-
Do you really think it won't work? in the class? How about this? https://3v4l.org/3Z6HE – Benyamin Limanto Apr 30 '19 at 04:56
PhpStorm supports code folding similar to C#:
#region [comment about what the code does]
code....
#endregion

- 1,262
- 2
- 15
- 25
If you are using notepad++ you can define that sort of thing with the ide in user defined language dialog, and since good old region starts with the pound sign, it works great because the pound "#" is actually one way to mark in php a comment. But, I tried it in version 6.6.6 of notepad++ and it already has #region #endregion functionality already no need to do anything.

- 59
- 1
- 3
You might be able to just use curly brackets {}, but it really depends on your editor.
I know some editors might pick that up as a code block, allowing you to collapse it, but it probably won't allow you to name the section that you're collapsing purely because that function isn't native to the language.

- 465
- 3
- 15
K.I.S.S. solution:
{ // region name, description, etc.
code;
}
Or, comment above the brace... Braces don't need no function, man.

- 19
- 1
I'm using Geany in Linux to do my PHP Programming, and it DO support the #region and #endregion to mark regions of code, and it works just like the MS Visual Studio to in C# language.
PHP Comments can be done using //, /* and */, and # too, so the PHP will just bypass these lines started with # and it's up to your text editor/IDE to make use of this... And Geany does!

- 498
- 1
- 4
- 15
I know it's an old thread, but in case you are interested.
I have successfully achieved #region / #endregion code folding in emacs, by adapting this code sample to php mode: http://blogs.msdn.com/b/dotnetinterop/archive/2008/04/14/making-hideshow-el-work-with-csharp-mode-el-and-region-endregion.aspx
The result is here:
; ===== PHP style region folding
(defun php-hs-forward-sexp (&optional arg)
"I set hs-forward-sexp-func to this function.
I found this customization necessary to do the hide/show magic in PHP
code, when dealing with region/endregion. This routine
goes forward one s-expression, whether it is defined by curly braces
or region/endregion. It handles nesting, too.
The forward-sexp method takes an arg which can be negative, which
indicates the move should be backward. Therefore, to be fully
correct this function should also handle a negative arg. However,
the hideshow.el package never uses negative args to its
hs-forward-sexp-func, so it doesn't matter that this function does not
do negative numbers.
The arg can also be greater than 1, which means go forward
multiple times. This function doesn't handle that EITHER. But
again, I haven't see that as a problem."
(message "php-hs-forward-sexp, (arg %d) (point %d)..."
(if (numberp arg) arg -1)
(point)
)
(let ((nestlevel 0)
(mark1 (point))
(done nil)
)
(if (and arg (< arg 0))
(message "negative arg (%d) is not supported..." arg)
;; else, we have a positive argument, hence move forward.
;; simple case is just move forward one brace
(if (looking-at "{")
(forward-sexp arg)
; The more complex case is dealing with a "region/endregion" block.
; We have to deal with nested regions!
(and
(while (not done)
(re-search-forward "^[ \\t]*#[ \\t]*\\(region\\|endregion\\)\\b"
(point-max) 'move)
(cond
((eobp)) ; do nothing if at end of buffer
((and
(match-beginning 1)
;; if the match is longer than 6 chars, we know it is "endregion"
(if (> (- (match-end 1) (match-beginning 1)) 6)
(setq nestlevel (1- nestlevel))
(setq nestlevel (1+ nestlevel))
)
)))
(setq done (not (and (> nestlevel 0) (not (eobp)))))
); while
(if (= nest 0)
(goto-char (match-end 2))
)
))
)
)
)
(unless (assoc 'php-mode hs-special-modes-alist)
(push '(php-mode
; "\\(^\\s*#\\s*region\\b\\)\\|{" ; regexp for start block DID NOT WORK
"\\(^[ \\t]*#[ \\t]*region\\b\\)\\|{" ; regexp for start block
; "\\(^\\s*#\\s*endregion\\b\\)\\|}" ; regexp for end block NO WORKY!
"\\(^[ \\t]*#[ \\t]*endregion\\b\\)\\|}" ; regexp for end block
"/[*/]" ; regexp for comment start
php-hs-forward-sexp ; hs-forward-sexp-func
hs-c-like-adjust-block-beginning ; c-like adjust (1 char)
;php-hs-adjust-block-beginning ; php adjust ?
)
hs-special-modes-alist)
)
;;
;; To use this, put this into your php-mode-hook:
;;
; for hide/show support
(add-hook 'php-mode-hook 'php-region-mode-stuff)
(defun php-region-mode-stuff ()
(hs-minor-mode 1)
(setq hs-isearch-open t)
; with point inside the block, use these keys to hide/show
(local-set-key "\C-c>" 'hs-hide-block)
(local-set-key "\C-c<" 'hs-show-block)
)

- 21
- 4
-
Ok, I have included the code in the answer. Thanks for the advice. – Andre Rodier Apr 11 '15 at 13:13
In Sublime Text there's a package called SyntaxFold.
Installation
- Ctrl+Shift+P type install and select Package Control: Install Package;
- In the new menu type SyntaxFold, begin the download and installation by selecting it;
- Check on the status bar below if it was installed correctly.
Configuration
- Press Shift+F5 to pop-up the fold panel and select Add Another to accesss the config file;
Ensure that it has a default with the strings you want (for some odd reason mine had a quote before the hash:
'#Start
). I ended up changing it to:{ //you can change in here other config options "default": { "endMarker": "#EOB", "name": "Default", "startMarker": "#BOB" } }
You can also alter the key bindings (the ones below in the Usage section) on Windows by changing them in the file:
C:\Users\YOUR_USER\AppData\Roaming\Sublime Text 3\Packages\SyntaxFold\Default (Windows).sublime-keymap
Usage
Add the enclosing tags you defined in the config file (you can add a description after each tag), in my case:
#BOB pandora box public function alpha() { # code... } public function omega() { # code... } #EOB pandora box
- To hide the code between the tags, place the cursor between them and press Alt+1 twice.
- To show it again, place the cursor on the first tag and press Alt+Shift+1 twice.
Alternative for Comment blocks
If you are just interested in folding comment blocks then with Fold Comments package you can press Ctrl+Shift+C to hide and show all comments (it may hide tags of SyntaxFold though).

- 4,663
- 5
- 45
- 60
In Visual Studio Code, you can "feel at home" with c# style regions like this:
#region Constants
const ICEBERGS_PER_TITANIC = 100000000;
const COFFEE_IV_DRIP_RATE = 0.00005;
#endregion
Technically, the region Constants
isn't required, all it needs is the # sign. But for c# developers who are used to seeing this (because Visual Studio insists on it being this way), it's not really noisy. And it's actually self-documenting. A PHP maintenance programmer would not by stymied by this.
Having said that, these ways also work, but it would be confusing (or just less clear) and might not work in another editor:
#deathstar Constants
const ICEBERGS_PER_TITANIC = 100000000;
const COFFEE_IV_DRIP_RATE = 0.00005;
#deathstar
# Constants
const ICEBERGS_PER_TITANIC = 100000000;
const COFFEE_IV_DRIP_RATE = 0.00005;
#

- 20,682
- 14
- 97
- 107
Now you can use #region on VSCode https://twitter.com/daviwil/status/914925309220106241

- 106
- 1
- 5
The # symbol can be used for a comment line as described in the documentation. You can gain this feature with a plugin for the folding feature.
In Visual Studio Code, I am using the PHP Region extension.

- 775
- 2
- 13
- 37
Workaround
- if(true){
// some code
}
so you can click on dash to collapse the section. Another possibility:
$region = 'load_parameters';
if($region == 'load_parameters'){
// some code
}

- 1,964
- 2
- 15
- 21
Although this is an old thread, but if one is still interested one can set a shortcut to code folding in NetBeans. This answer extends @krtek 's answer. To achieve this follow the steps:
Go to Tools> Options> Editor> Code templates
Click New, enter "cf" as the abbreviation, then enter the following code as the expanded text:
// <editor-fold defaultstate="collapsed" desc=" ${DESCRIPTION} ">
${selection line}${cursor}
// </editor-fold>
- In the description tab, enter "Code folding", and click Ok.
Now, if you select multiple lines of text in the editor, you will get the "light bulb" icon in the margin. Click it, and you'll get the option "Surround with Code folding". Select it, enter a description and you're done.
For reference see this link: reference

- 49
- 8
old question, but now for users of VS Code which is becoming popular for PHP development, the plugin Region Folder does the trick perfectly and flexibly
https://marketplace.visualstudio.com/items?itemName=maptz.regionfolder

- 2,097
- 3
- 27
- 50