- 필수 기능
- 시작하기
- Glossary
- 표준 속성
- Guides
- Agent
- 통합
- 개방형텔레메트리
- 개발자
- API
- Datadog Mobile App
- CoScreen
- Cloudcraft
- 앱 내
- 서비스 관리
- 인프라스트럭처
- 애플리케이션 성능
- APM
- Continuous Profiler
- 스팬 시각화
- 데이터 스트림 모니터링
- 데이터 작업 모니터링
- 디지털 경험
- 소프트웨어 제공
- 보안
- AI Observability
- 로그 관리
- 관리
ID: php-best-practices/avoid-yoda-conditions
Language: PHP
Severity: Notice
Category: Best Practices
In PHP, it is a recommended practice to put constants and literals on the right side of comparison operators such as ==
or ===
. This rule is important because it helps prevent potential bugs caused by accidental assignment instead of comparison. Accidentally writing =
instead of ==
is a common mistake, but if the constant is on the left side, PHP will throw an error, making the problem apparent.
Non-compliant code may still function as expected, but it can lead to confusion and hard-to-spot bugs, especially in large codebases or among teams of developers. It is also not considered a best practice in terms of code readability and maintainability.
To avoid this, always place your constants or literals on the right side of your comparison operators. This not only aligns with common practice, but also it allows PHP’s own error handling to assist in catching any accidental assignment errors. This can increase the robustness of your code and decrease debugging time.
<?php
if (51 == something) {
}
if ("myValue" == something) {
}
if (0.0 == value && 0 == plop) {
}
<?php
if (something == 51) {
}
if (something == "myValue") {
}
if (value == 0.0 && plop == 0) {
}
|
|
For more information, please read the Code Analysis documentation
Identify code vulnerabilities directly in yourVS Code editor
Identify code vulnerabilities directly inJetBrains products