PHP var declaration best practices

545 Views Asked by At

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.

2

There are 2 best solutions below

2
On BEST ANSWER

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

0
On

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