-1

I always try to code with the best practices possible, and regarding PHP, I use mainly the PSR-12 standards.

Now regarding var declarations, is it "wrong" to use tabs like the example below?

$a      = 1;
$abc    = 2;
$abdef  = 3; 

Or should I keep without tabs?

$a = 1;
$abc = 2;
$abdef = 3; 

I'm asking this because the first option looks more readable for the developer.

Tiago Mateus
  • 356
  • 4
  • 17
  • 1
    Check this: https://stackoverflow.com/questions/32787223/phpstorm-9-align-assignments-in-php – Ricardo Andrónico Dec 18 '20 at 10:27
  • 1
    Thanks @RicardoAndrónico but this is more related with a project itself and not the IDE. Nevertheless, thanks for the link! – Tiago Mateus Dec 18 '20 at 10:30
  • 1
    I actually find the first solution less readable. Your eyes have to track the line to see the value. But opinions aside, the first solution - unlike the second one - requires maintenance. If you add/remove a variable with a longer name, then you have to correct the tabs for all the other lines. – El_Vanja Dec 18 '20 at 11:14

2 Answers2

1

It depends from the codestyle of the project. The worth case if in one file you'll have variables with tabs, and in second without.

Second option is more classic, so I advice you to use it. Also you can look to some big opensource projects like symfony or laravel, and try to make it in similar way :)

https://github.com/laravel/laravel

https://github.com/symfony/symfony

Andrii Sukhoi
  • 749
  • 5
  • 14
  • I see your point, but what about the standards? – Tiago Mateus Dec 18 '20 at 10:24
  • 2
    There are a lot of standards, mostly psr12 is in use currently, but as I said it depends from project, if you use Symfony, the best is to follow symfony stylegujde, if you use for example wordpress, it has it's own standard. It is not only about tabs when you declaring variables :) – Andrii Sukhoi Dec 18 '20 at 10:53
1

In my opinion, to avoid longer variables definitions and in case of an array with key=>value, and variable name will be the key from array, I will prefer this: https://www.php.net/manual/en/function.list.php

Reference: https://code.tutsplus.com/tutorials/working-with-php-arrays-in-the-right-way--cms-28606