PHP Coding Standard

please click here for more wordpress cource

PHP has several coding standards that developers can choose to follow. The most widely used standard is the PSR-1 and PSR-2 standards, which were created by the PHP-FIG (Framework Interop Group). Here are some of the key guidelines from these standards:

  1. PSR-1:
  • Files must use only <?php and <?= ?> tags.
  • Files must use UTF-8 encoding without BOM.
  • Files must declare namespace at the top.
  • Class names should be declared in CamelCase.
  • Class constants must be all uppercase with underscores separating words.
  • Method names should be declared in camelCase.
  1. PSR-2:
  • Code must follow PSR-1 guidelines.
  • Indentation must be done with 4 spaces, not tabs.
  • There must be one blank line before the namespace declaration, and there must be one blank line after the block of use statements.
  • There must be one blank line after the docblock preceding a class, property, or method declaration.
  • Opening braces for classes, methods, and control structures must go on the same line as the statement.
  • Control structure keywords must have one space after them, and opening parentheses must not have a space after them.
  • Method arguments must have a space after the comma.
  • Closing braces must be on a new line after the body of a class, method, or control structure.

Following these standards can help make your code more readable and maintainable, and it can also make it easier for other developers to understand your code. There are also several code analysis tools that can help enforce these standards, such as PHP Code Sniffer.

You may also like...

Popular Posts

Leave a Reply

Your email address will not be published. Required fields are marked *