Search code examples
phpstorm

How in PhpStorm selecting some var in focus to add/remove “$” symbol in the begging of the var?


Is there a way in PhpStorm for selecting some var in focus to add/remove “$” symbol in the begging of the var? This functionality seems very good in declaring vars like :

protected ContactUs $contactUs;
protected User $manager;

public function __construct(ContactUs $contactUs, User $manager)
{
    $this->contactUs = $contactUs;
    $this->manager = $manager;
}

PhpStorm 2023.3.5


Solution

  • To select a PHP variable without the leading $, you can enable the following option: Settings/Preferences | Editor | General | Smart Keys | PHP | Select variable name without '$' sign on double click

    enter image description here


    To add/remove the $ symbol from a variable though (more than one place at once, as I understand)... My best guess is to use multiple carets:

    • select one variable first and then use "Find | Add Selection for Next Occurrence" (Alt + J here on Windows keymap) to add other matches one by one
    • alternatively can add multiple carets in all places manually as needed (without doing a selection) with Alt + Click.

    Then just edit as if it's a single place. Press Esc to exit the multi caret mode when finished.

    enter image description here


    Unless I've got it wrong and you have something else in mind. In this case you need to clarify your request: what you have now and what you want to have in the end -- so that it should clearly show the difference: "before and after". It is possible that there could be more suitable ways (e.g. a quick fix/intention already available etc.)