2023-05-03 11:06:20 +02:00
== Why is this an issue?
2023-05-12 16:21:47 +02:00
Boolean operators are used to combine conditional statements based on their value.
In PHP, there are two different sets of operators to use for AND and OR:
2021-04-28 16:49:39 +02:00
2023-05-12 16:21:47 +02:00
* ``++&&++`` / ``++||++``
* ``++and++`` / ``++or++``
The difference between these sets is the precedence, which specifies how "tightly" two expressions are bound together.
Because ``++and++`` / ``++or++`` have a lower precedence than almost any other operator, using them instead of ``++&&++`` / ``++||++`` may not have the result you expect.
2021-04-28 18:08:03 +02:00
2023-05-03 11:06:20 +02:00
=== Noncompliant code example
2021-04-28 16:49:39 +02:00
2023-05-12 16:21:47 +02:00
In both cases, the assignment has a higher precedence over the boolean operation.
[source,php,diff-id=1,diff-type=noncompliant]
2021-04-28 16:49:39 +02:00
----
2023-05-12 16:21:47 +02:00
$resultAnd = true and false; // Noncompliant: $resultAnd == true
2021-04-28 16:49:39 +02:00
2023-05-12 16:21:47 +02:00
$resultOr = false or true; // Noncompliant: $resultOr == false
----
2021-04-28 18:08:03 +02:00
2023-05-03 11:06:20 +02:00
=== Compliant solution
2021-04-28 16:49:39 +02:00
2023-05-12 16:21:47 +02:00
[source,php,diff-id=1,diff-type=compliant]
2021-04-28 16:49:39 +02:00
----
2023-05-12 16:21:47 +02:00
$resultAnd = true && false; // $resultAnd == false
$resultOr = false || true; // $resultOr == true
2021-04-28 16:49:39 +02:00
----
2021-04-28 18:08:03 +02:00
2023-05-12 16:21:47 +02:00
== Resources
2021-04-28 18:08:03 +02:00
2023-05-12 16:21:47 +02:00
=== Documentation
* https://www.php.net/manual/en/language.operators.logical.php[PHP Manual - Logical Operators]
* https://www.php.net/manual/en/language.operators.precedence.php[PHP Manual - Operator Precedence]
2021-06-02 20:44:38 +02:00
2021-06-03 09:05:38 +02:00
ifdef::env-github,rspecator-view[]
2021-09-20 15:38:42 +02:00
'''
2023-05-12 16:21:47 +02:00
2021-09-20 15:38:42 +02:00
== Implementation Specification
2023-05-12 16:21:47 +02:00
2021-09-20 15:38:42 +02:00
(visible only on this page)
2023-05-25 14:18:12 +02:00
=== Message
Replace "xxx" with "yy".
2021-09-20 15:38:42 +02:00
2021-06-08 15:52:13 +02:00
'''
2023-05-12 16:21:47 +02:00
2021-06-02 20:44:38 +02:00
== Comments And Links
2023-05-12 16:21:47 +02:00
2021-06-02 20:44:38 +02:00
(visible only on this page)
2023-05-25 14:18:12 +02:00
=== is related to: S3659
2021-06-03 09:05:38 +02:00
endif::env-github,rspecator-view[]