• 15
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

Is it possible to auto format PHP in Sublime Text 2?

In Sublime Text 2, I am trying to type the code and have auto format command to rearrange and auto indent source code. If it is not possible to have a automatic formatter, would be nice to have a shortcut command to do it.

I am searching for something else besides the built-in "Reindent" option and the hard-to-make-work "PHP Beautifier" plugin.

I would like to know if someone found any nice auto formatter for PHP: any formatter that won't mess our PHP code. JSFormat seems to be not useful.

Is there any easy to "install and use" plugin?

PHPTidy is definitely good choice for PHP code formatting in SublimeText2.

You need to install package control package to enable you to install packaged in sublime text: link.

  1. ctrl+shift+p -> type -> Package Control: Install Package

  2. search for phpTidy -> hit enter

that is it - now you can format your code in WordPress coding convention.

How to format you code:

  1. ctrl+shift+p -> type -> tidy -> Hit Enter
  2. ctrl+alt+t

Hope this help.

  • 39
Reply Report

There is a PHP code formatter named PhpBeautifier. Here you can find a Sublime Text plugin for this program and you can also install it via package control.

  • 20
Reply Report
      • 2
    • I knew that PhpBeautifier. After installing it on lastest Sublime Text 2, I had so many formatting issues that I uninstalled it. I am still wishing to find another plugin that formats my code without messing with it. Javascript formatters seems to work, but this PhpBeautifier drove me crazy.

I have this set up as a keybind:

[
    {
      "keys": ["alt+shift+f"], "command": "reindent",
      "args": {"single_line": false}
    }
]

If you're looking for a bit more than that there is Tidy PHP under package management.

Update: Been some time, I'm now using PHP Coding Standards along with PHP CS Fixer which automates a lot of the cleanup done by autoformatting. This takes a fair bit of work / understanding of php cs to setup but its not too hard, and well worth it. Link

  • 7
Reply Report
      • 1
    • Sadly the built in reindent can't handle single line comments with // and can't format arrays that are longer that one line

I've been searching this before also, but with no avail. Sublime text 2 probably doesn't support formatting PHP code natively. I have looked into ST2 packages - http://wbond.net/sublime_packages/community, but there is also nothing. Probably you'll have to do this in another editor.

Of course it would be nice, if someone would write a package for this, 'cause packages for formatting JS, JSON, HTML.. already exist.

  • 6
Reply Report

Not sure if this is what you are looking for, but are you aware that when you save your file as .php, Sublime Text 2 automatically highlights and indents according to its built in PHP rules. You can also select this from going to View, and then Syntax, and selecting PHP. But again, this is automatically done when you save the file as .php.

  • 0
Reply Report

Warm tip !!!

This article is reproduced from Stack Exchange / Stack Overflow, please click

Trending Tags

Related Questions