Search code examples
phpstorm

Shorthand for generating for loop in PhpStorm


In PhpStorm, you can type forek or fore + TAB and you'll get

foreach (| as $index => $index) {

}

and

foreach (| as $item) {

}

respectively.

In Android Studio (Java), you can type fori + TAB and you'll get

for (int i = 0; i < |; i++) {

}

| is where the cursor ends up.

I was wondering if PhpStorm has a similar way of generating for structures. I've been trying fori but to no avail. Seems rather curious that forek and fore are included but not the classic for with a counter.

Perhaps the shortcut is different?


Solution

  • PhpStorm only has predefined live templates for foreach PHP loops. But you can easily create the corresponding template yourself in Settings | Editor | Live Templates (https://confluence.jetbrains.com/display/PhpStorm/Live+Templates+(Snippets)+in+PhpStorm), like

    for($INDEX$ = 0; $INDEX$ < $LIMIT$; $INDEX$++) {
      $END$
    }
    

    where expression for $INDEX$ is phpSuggestVariableName()

    enter image description here