Editing with PHPStorm

Tags

PHPStorm is freakin' amazeballs. Let's see how it can help you edit your code.

Here's some code from before. Make a new PHP file in PHPStorm, and paste it in:

  • <?php
  • $dogs = $_GET['dogs'];
  • // Compute message, and CSS class.
  • $message = '';
  • $class = '';
  • if ($dogs < 1) {
  •   $message = "You don't have doggos! Boo! Get doggos!";
  •   $class = 'argh';

First thing you notice, PHPStorm has indented some of it:

Old code

Indenting is not optional in this course. You must indent code in ifs and loops, as well as function definitions (more on that later).

Check out the tab. The file name is underlined. If you see that, it means PHPStorm has seen something it things is an error. Don't ignore these reports. PHPStorm gets it wrong sometimes, but not often.

When I hit enter and added the close brace (})...

Closed brace

... PHPStorm snapped it to the left, adjusting indenting automatically.

Neato keen!

Now I hit enter, and type "el":

Autocomplete

PHPStorm shows a list of symbols it knows that begin with "el". It puts the things it thinks you most likely want at the top of the list. It saw the if above, so it shows "else" and "elseif" first.

I press the down arrow, hit enter, and PHPStorm fills in the "elseif." It also adds some parentheses, and puts the cursor inside them, since that's what I'll need.

elseif selected

Groovy!

I type "d". PHPStorm's autocomplete kicks in:

Autocomplete again

Notice that $dogs is near the top of the list. PHPStorm analyzed your code, picked out your variables, and added them to its autocomplete list.

Text with a yellow background means a warning. Put the mouse cursor on the yellow, and you get a tooltip with the deets.

Warning

There's MUCH more the editor can do. One of my fave PHPStorm tricks is variable renaming. Put the cursor in a variable, then hit Shift+F6.

Variable renaming

PHPStorm lets you type in a new name for the variable.

New name

Click Refactor (a geek word for code redesign), and PHPStorm changes all instances of the name.

Changed

Ethan
Ethan

Isn't that the same as search-and-replace?

Not quite. Search-and-replace would have replaced instances of dogs in output messages as well. I didn't want that. I just wanted to change the variable names.

BTW, naming variables well is very important. It helps makes your code easy to maintain. Make the names of variables match what they mean. So $salesTaxRate is better than $sr.

Good variable names are not optional in the course. Like indenting, they're required.

PHPStorm is popular with professional PHP devs. Try working through some tutorials on the interwebs. There are lots.