Closing Tag Omission for PHP Files
-
Rule: If a PHP file contains only PHP code, the closing PHP tag (
?>) MUST be omitted. -
Reason: The closing tag is not necessary when the file only contains PHP code. Omitting it helps to avoid accidental whitespace or newlines after the PHP code, which could lead to issues like headers already being sent.
Strict Typing Declaration
-
Rule: PHP files SHOULD declare strict types as
1(by addingdeclare(strict_types=1);at the beginning of the file), whenever possible. -
Reason: Enabling strict typing helps catch type-related errors early by enforcing stricter type checks at runtime. It improves code clarity and consistency.
-
However, strict typing MUST NOT be mixed with older codebases that don’t follow this practice. This means that if you're working in a codebase that does not use strict types, you shouldn't introduce
declare(strict_types=1);to individual files as it could lead to inconsistencies or errors.
Example:
<?php
declare(strict_types=1);
// Your PHP code here
?>