PhpStorm
 
Get PhpStorm
You are viewing the documentation for an earlier version of PhpStorm.

Extract/Introduce variable

Last modified: 15 September 2023

The Extract Variable refactoring puts the result of the selected expression into a variable. It declares a new variable and uses the expression as an initializer. The original expression is replaced with the new variable (see the examples below).

To perform this refactoring, you can use:

  • In-place refactoring. In this case you specify the new name right in the editor.

  • Refactoring dialog, where you specify all the required information. To make such a dialog accessible, open the Settings dialog (CtrlAlt0S), go to Editor | Code Editing, and select the In modal dialogs refactoring option in the Refactorings area.

You can select the expression to be replaced with a variable yourself. You can as well use smart expression selection. In this case PhpStorm will help you select the desired expression.

This refactoring is also available for JavaScript and Style Sheets.