Skip to content

spacing.operatorsfixable

Ensure consistent operators spacing

Examples

  • Examples of correct code for this rule using default options

Comparisons and logical operators with correct spacing

php
<?php
if ($value === TRUE) {
} else if ($value === FALSE) {
}

if ($value == TRUE) {
} else if ($value == FALSE) {
}

if (is_array($array) <> TRUE) {
} else if (myFunction($value) > FALSE) {
}

if (is_array($array) == TRUE) {
} else if (myFunction($value) == FALSE) {
}

if ($value === TRUE || $other === FALSE) {
}

if ($value == TRUE || $other == FALSE) {
}

if ($value || !$other) {}

if (
	$one === TRUE ||
	$two != 17  OR
	$three >= 5
    && $four === TRUE) {
}

if ($one || $two  AND   !$three || $four) {
}

if ($one
|| $two
&& !$three ||
$four) {
}

if (false === ($parent instanceof Foo) && ($parent instanceof Bar) === false) {
}

if (false === ($parent instanceof Foo) or $foo) {
}

while ($var1 === TRUE) {
}

do {

} while ($var1 <=> TRUE);

for ($var1 = 10; $var1 !== 0; $var1--) {
}

for ($var1 = ($var2 === 10); $var1; $var1--) {
}

if (empty($argTags > 0)) {
}

if (empty($argTags 
< 0)) {
}

Ternary, instanceof operators with correct spacing

php
<?php

if ($var instanceof PHP_CodeSniffer) {
}

if (($var instanceof PHP_CodeSniffer) === false) {
}

if ($good && ($var instanceof PHP_CodeSniffer) === false && $good) {
}

if ($good === true && ($var instanceof PHP_CodeSniffer) === false) {
}


$var1 === TRUE
    ? $var2 = 0
    : $var2 = 1;

$var1 === TRUE ? $var2 = 0 : $var2 = 1;

$var1 
=== TRUE ? $var2 = 0 : $var2 = 1;

if ($var2 === TRUE) {
    $var1 === TRUE ? $var2 = 0 : $var2 = 1;
}

$var1 === TRUE ? $var2 = 0 : $var2 = 1;

$var1
    ? $var2 = 0 : 
	$var2 = 1;

($var1 === true) ? $var2 = 0 : $var2 = 1;


$var1 ? $var2 = 0 : $var2 = 1;

if ($var2 === TRUE) {   $var1 ? $var2 = 0 : $var2 = 1;
}

$var1 === TRUE ?: $var2;
$var = ($var1) ?: "foobar";
$var = ($var1)
?: "foobar";

$var = $var1 ?: $test ?: $test2;
$var = $var1 ?: $test ? $test2 : $test3;


myFunction($var1 === true ? "" : "foobar");

Arithmetic, bitwise, null coalescing operators with correct spacing

php
<?php
$a >> $b;
$a << $b;
$a | $b;
$a & $b;
$a ^ $b;
$a ?? $b;
$a xor $b;

Incrementing/Decrementing, string and unary operators with correct spacing

php
<?php
$a++;
$b--;
++$c;
--$d;

$a + +$b;
$a + -$b;

-$c;
+$d;

if(!$a && !$b){
    
}

~$a;

$b = $a . "World!";
$c = $a . "World!";
$d = $a . "World!"  ;

Reference (&), variadic (...), type (😃, arrow function (=>) operators with correct spacing

php
<?php

function($param, $param1): boolean{};
function(): float{};
function($param): 
int{};

function
($param
): 
int{};

function($param, $param1): void{};
function($param, $param1): string|int {};
function($param, $param1): ReturnType&AnotherType {};

function(&$first, ? number $nullable, ...$rest): number{}
fn(&$first, ? number $nullable, ...$rest): number 
 => $x;

fn($x) => $x;

$fn1 = fn($x) => $x + $y;
$fn = fn($x) => fn($y) => $x * $y + $z;

fn(): int => $x;
fn(&$x) => $x;
fn&($x) => $x;
fn($x, ...$rest)
 => $rest;
  • Examples of incorrect code for this rule using default options

Comparisons and logical operators with incorrect spacing

php
<?php
if ($value===TRUE) {
} else if ($value === FALSE) {
}

if ($value==TRUE) {
} else if ($value==FALSE) {
}

if (is_array($array)  <>TRUE) {
} else if (myFunction($value)>FALSE) {
}

if (is_array($array)== TRUE) {
} else if (myFunction($value) ==FALSE) {
}

if ($value===    TRUE||$other    ===FALSE) {
}

if ($value     ==TRUE    ||$other== FALSE) {
}

if ($value||!$other) {}

if (
	$one=== TRUE||
	$two !=17  OR
	$three >= 5
    &&$four=== TRUE) {
}

if ($one||$two  AND   !$three||$four) {
}

if ($one
||$two
&&     !$three||
$four) {
}

if (false === ($parent instanceof Foo)   &&($parent instanceof Bar) === false) {
}

if (false === ($parent instanceof Foo)  or  $foo) {
}

while ($var1 === TRUE) {
}

do {

} while ($var1<=>TRUE);

for ($var1 = 10; $var1!==0; $var1--) {
}

for ($var1 = ($var2===    10); $var1; $var1--) {
}

if (empty($argTags > 0)) {
}

if (empty($argTags 
<0)) {
}

Ternary, instanceof operators with incorrect spacing

php
<?php

if ($var instanceof     PHP_CodeSniffer) {
}

if (($var     instanceof PHP_CodeSniffer)===    false) {
}

if ($good&&($var instanceof PHP_CodeSniffer)===  false&&    $good) {
}

if ($good ===true&&   ($var   instanceof PHP_CodeSniffer)=== false) {
}


$var1 ===    TRUE
    ?$var2 = 0
    :   $var2 = 1;

$var1   === TRUE ?    $var2 = 0  :$var2 = 1;

$var1 
===TRUE ? $var2 = 0 : $var2 = 1;

if ($var2===  TRUE) {
    $var1===TRUE?$var2 = 0:$var2 = 1;
}

$var1===TRUE?    $var2 = 0:     $var2 = 1;

$var1
    ?$var2 = 0: 
	$var2 = 1;

($var1 === true)?$var2 = 0    :$var2 = 1;


$var1 ? $var2 = 0 : $var2 = 1;

if ($var2===TRUE) {   $var1?  $var2 = 0:  $var2 = 1;
}

$var1 === TRUE   ?:$var2;
$var = ($var1)?:"foobar";
$var = ($var1)
?:"foobar";

$var = $var1?:$test ?:$test2;
$var = $var1  ?:$test   ?$test2:    $test3;


myFunction($var1 === true?""   :"foobar");

Arithmetic, bitwise, null coalescing operators with incorrect spacing

php
<?php
$var = $a +  $b;
$var = $a  /$b;
$var= $a-  $b;
$var =   $a *$b;
$var= $a%$b;
$var =  $a  **$b;
$var= $a.$b;
$var = $a.$b.  $c;
$var = $a/$b*$c + $d;
$var= $a +  $b/$c*$d;

$var=  ($a +$b)%$c*$d  -$e;
$var= $a +$b%$c**$d- $e;

$var =  $a+ 
    $b %  $c*$d  -$e;

$a    +$b%$c*$d - $e;

$var = ($b = 4) + 5;
$var /= $a + $b;
$var %=$a ** $b;

$a>>$b;
$a<<$b;
$a|$b;
$a&$b;
$a^$b;
$a??$b;
$a xor  $b;

Incrementing/Decrementing, string and unary operators with incorrect spacing

php
<?php
$a ++;
$b --;
++ $c;
--   $d;

$a + + $b;
$a + - $b;

- $c;
+   $d;

for ($n = 0; $n < 6; $n ++) {
    echo ++ $s;
}

if(! $a&&!$b){
    
}

~ $a;

$b = $a."World!";
$c = $a  ."World!";
$d = $a   .  "World!"  ;

Reference (&), variadic (...), type (😃, arrow function (=>) operators with incorrect spacing

php
<?php

function($param, $param1)  :     boolean{};
function()    : float{};
function($param)   : 
int{};

function
($param
)   : 
int{};

function($param, $param1):void{};
function($param, $param1):string|int {};
function($param, $param1) :ReturnType&AnotherType {};

function(&$first, ? number $nullable, ...$rest):number{}
fn(&$first, ? number $nullable, ...$rest):number 
 => $x;

fn($x) => $x;

$fn1 = fn($x)=>  $x + $y;
$fn = fn($x)=>fn($y) => $x * $y + $z;

fn(): int=>  $x;
fn(&$x)=>$x;
fn&($x)=>  $x;
fn($x, ...   $rest)
=>$rest;

Released under the MIT License.