PHP Syntax Check in Sublime Text Editor
Asked Answered
L

1

5

In Gedit, I can add an External Tool of "php -l" on the current document, and if I have PHP command line installed, it will syntax check the document. Is there a way to do this with Sublime Text Editor? (Note, I have a Mac and it has PHP CLI installed.)

I imagine I'll have to paste a code snippet into Sublime plugin, right?

Lolitaloll answered 26/6, 2014 at 14:39 Comment(1)
A queck check of google reveals: sublime.wbond.net/packages/PHP%20Syntax%20CheckerCosignatory
S
13

The action you are referring to is called "linting" and there are a number of plugins for Sublime that lint PHP files. As mentioned by Len_D, PHP Syntax Checker is one, but I'd actually recommend SublimeLinter for Sublime Text 2 instead. (There is a different version of SublimeLinter for ST3, but it's not backwards-compatible, and has a completely different architecture than the ST2 version, which is no longer officially supported.)

To install, first install Package Control if you haven't already, then restart Sublime. Open the Command Palette with CtrlShiftP and type pci to bring up Package Control: Install Package. Hit Enter, then type in sublimelinter, then hit Enter again to install. After installation is complete, restart ST2 again for good luck. To configure, first open Sublime Text 2 -> Preferences -> Package Settings -> SublimeLinter -> Settings-Default and copy its entire contents. Then, open Settings-User from the same sub-menu and paste the contents into it. You can now close Settings-Default. For proper syntax highlighting (to tell where the comments are), select JavaScript -> JSON from the option list in the lower right of the Sublime window.

Scroll down to line 36 in the "sublimelinter_executable_map" dict and add a blank line between the opening { and closing } braces. Find the full path to the php executable on your system by opening Terminal and typing which php. Copy the path and add an entry for "php" on the blank line you just made. For example, if the path is /opt/local/bin/php, the full section should look like this:

"sublimelinter_executable_map":
{
    "php": "/opt/local/bin/php"
},

Scroll down through the rest of the "sublimelinter_*" options and modify them to suit your preferences. The options after line 108 are most likely irrelevant for you, as they deal with linters for JavaScript, CSS, Python, etc. However, feel free to read through them in case you'd like to use SublimeLinter for other languages. Once you're done, save the file and you should be all set. SublimeLinter will display its messages according to the "sublimelinter" (line 13) and "sublimelinter_delay" settings (line 67) (increase the value to increase the delay between stopping typing and linter messages appearing). If you don't want this "live" linting, set "sublimelinter" to load-save, save-only, or false, depending on your preferences. I personally find live linting to be rather annoying...

And that's about it. Full documentation is available in the README. Please keep in mind that if/when you upgrade to Sublime Text 3 (which I highly recommend, by the way), you'll need to install and configure SublimeLinter3, which is a complete re-write of the plugin into a more modular architecture. As such, the base SublimeLinter package must be supplemented by a language-specific linter plugin like SublimeLinter-php. Please ensure you read the full documentation (yes, there's a lot, but it's worth it) to get everything running smoothly.

Good luck!

Shamrao answered 26/6, 2014 at 17:25 Comment(1)
I installed this on Sublime Text 2 on Windows 7 and I didn't need to change any settings. Nice.Nomadic

© 2022 - 2024 — McMap. All rights reserved.