PHP coding style for non-operator

I and I obviously have different styles! operator in PHP. I usually write

if (!$x) { $y = 2; } 

But he prefers distance! from a variable.

 if (! $x) { $y = 2; } 

What could lead to something like this

 if ($x && ! $y) { 

which looks weird to me.

Usually we try to follow PSR-2, but I am not an expert on this issue and have not found an explicit recommendation. For other types of operators, there is a space before and after

 if ($x === false) { 

and

 if ($x > $y) { 

Then there is also

 $x = & $y; 

I usually do not do this, so I have no opinion.

Can anyone specify a PSR requirement for! example? If not, is there another rule that should be followed? Our IDE formats are now dueling.

+6
source share
2 answers

First of all: do whatever you want.

Secondly, here are two style conventions from two large projects: Drupal :

Unary operators (operators that work with only one value), such as ++ , must not have a space between the operator and the variable or number on which they work.

and symfony :

Place the unary operators ( ! , -- , ...) next to the affected variable

So, the small requested example (just the first to appear on Google) did not register a place. Which, on a personal level, I would agree.

+12
source

Oh, don't discuss such minor conventions. If I read if (!$x) { or if (! $x) { , the first question is what is $ x? For me, $ x is a coordinating or, possibly, counter, so bool operations are similar to while($i--) {} saving an expensive byte, which makes the code easier to read or simply unsuccessful when calling this bool variable.

Many projects begin with the question of any recommendations or crazy heavy documentation and all. This is actually just a signal not to know how to start it.

If my team is writing code, it’s good to use it repeatedly and intuitively and possibly large integrated into the framework, then I have no problem reading heterogeneous code.

Another thing is writing methods, attributes and functions of small camel, const with capital letters and underscores, etc.

-3
source

Source: https://habr.com/ru/post/982118/


All Articles