How to indent PHP code in PHP-mode?

General Tech Bugs & Fixes 2 years ago

0 1 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Bugs & Fixes related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (1)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago

 

I come from Java/Netbeans, and I can indent my Java code using alt+shift+f, I have searched through this site and other articles, but I didn't find a good way to indent PHP.

What I found:

  1. using pear PHP_beautifier

but I have to output the result to a different file or simply copy on terminal result.

  1. using PHP beautifier on the web

I have tried using M-x indent-region in PHP-mode but nothing changes. I tried in jdee for Java code M-x indent-region also doesn't work.

I found other solutions but for JSON, com.lang, Asytle (C/C++/C#)

this is my .emacs

https://gist.github.com/azzamsa/1f76b8ca00fd2737ae08a5c3f6dc6b3f

I am using :

php-mode-1.18.2(ejmr/php-mode), GNU Emacs 24.4.1

and installed php-mode from elpa repo.

After following phils suggestion to use Tab, it works. But when I try to break the code format, and try to see the result using C-xh and Tab, it doesn't indent the code properly as what online formater does. e.g.:

    public function

        __construct($name) {
        $this->name = $name;
        //$this->numLeg = $numLeg;
    }

    public

        function

            bark() {
            return "Woof!";
        }
    public

        function

            greet() {
            return "hello i am " . "$this->name";
        }


}

There is no result even when I use indent-region. I also tried to use fill-region, and other choice of fill, but no luck. But the online formatter can handle this nicely, the result from online formatter and the expected one is:

class Dog {
    public $numLegs = 4;
    public $name;

    public function __construct($name) {
        $this->name = $name;
        //$this->numLeg = $numLeg;
    }

    public function bark() {
        return "Woof!";
    }
    public function greet() {
        return "hello i am " . "$this->name";
    }


}
$dog1 = new Dog("Barker");
$dog2 = new DOg("Amigo");

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.