Operators

Verze:

18. 06. 2025

Zodpovědná osoba:

Dominik Šlechta

Spacing Around Binary Operators

  • Rule: All binary arithmetic, comparison, assignment, bitwise, logical, string, and type operators MUST be preceded and followed by at least one space.

  • Reason: This ensures that operators are clearly separated from their operands, improving readability and consistency.

Example (Correct):

$a = $b + $c;
$result = $a == $b;

Example (Incorrect):

$a=$b+$c;
$result=$a==$b;

No Space Around Increment/Decrement Operators

  • Rule: The increment (++) and decrement (--) operators MUST NOT have any space between the operator and the operand.

  • Reason: This is the standard syntax for increment and decrement operations and ensures that the operators are correctly applied to the operands.

Example (Correct):

$i++;
--$i;

Example (Incorrect):

$i ++;
$i --;

No Space Within Type Casting Operators

  • Rule: Type casting operators (e.g., (int), (float), etc.) MUST NOT have any space within the parentheses.

  • Reason: This is the correct syntax for type casting in PHP, and it keeps the code clean and consistent.

Example (Correct):

$intValue = (int) $input;
$floatValue = (float) $value;

Example (Incorrect):

$intValue = ( int ) $input;
$floatValue = ( float ) $value;

Spacing Around Conditional (Ternary) Operator

  • Rule: The conditional (ternary) operator MUST be preceded and followed by at least one space around both the ? and : characters.

  • Reason: This improves readability by ensuring that the ternary operator is clearly separated from the operands.

Example (Correct):

$result = ($a > $b) ? $a : $b;

Example (Incorrect):

$result = ($a > $b)?$a:$b;

Summary:

  • Binary operators (arithmetic, comparison, assignment, bitwise, logical, string, type) MUST have spaces before and after them.

  • Increment/decrement operators (++, --) MUST NOT have spaces between the operator and operand.

  • Type casting operators MUST NOT have space within the parentheses.

  • Conditional (ternary) operator MUST have spaces around both ? and : characters.