PHP PSR is a PHP specification developed by the PHP-FIG organization, which is a practical standard for PHP development. The PHP-FIG organization was established in 2009 by several open source framework developers, although it is not an "official" organization. , but also represents the majority of the PHP community.

##PSR-1: Basic coding style
At the beginning of the article, We have already briefly introduced what PSR is. PSR is a PHP standard, and PSR-1 is the most basic and simplest standard for PHP.PHP tag
The code must be placed in the or ?> tag. Do not use other tag syntax I believe many PHPers are easy to comply with this, and in real code, normal tags## are generally used #EncodingAll PHP files must use UTF-8 character set encoding, and cannot have Byte Order Mark (Byte Order Mark, BOM)
This is also very common, that is BOM-free and BOM-based formats. I remember when I first started typing PHP code, my seniors were always very concerned about it, and I had to use the IDE to adjust it to the BOM-free format. I was confused at the time, and then I followed suit. Now that I see this, I started to do it again. After checking the information, I found the reason why the BOM format cannot be used. The BOM will produce redundant output, just like an extra blank line for no reason:
When php processes the BOM header, there are sometimes errors. , which may cause an error that the file has been output when you use functions such as header or session_start. Most of the time it is because the BOM header is sent out. . Because in php's view, it becomes a space. So use the BOM-free format
PurposeA PHP file can define symbols (classes, traits, functions, constants, etc.), or perform operations with side effects (generate result or process data), but cannot do two things at the same time
This rule basically means that a variable, method or class can only complete one operation and do one thing accordingly, thus ensuring the code It is clear and easy to understand, and it also ensures that the methods and variables are single and each performs its duties. In fact, it is also for convenience. When the project/application becomes larger in the future, we can decouple it well
Automatic loadingPHP namespaces and classes must comply with PSR -4 Autoloader standards
The name of the classThe name of the PHP class must use camel case, also known as title style
Camel case and word segmentation These two writing methods (each word is separated by _), I remember there was a lot of controversy in the past. Some people supported the camel case (GirlFriend), and some supported the word participle (girl_friend). Now it’s better, and they have been unified. In order to make PHP more If the development is good, then we will reluctantly support the participle-style brothers and unify the hump. I remember that the company's CI2 project used this kind of word segmentation, but it was also a framework requirement. Later, in my own project, I had a hunch and used the camel case, haha, thumbs up for my vision~
Names of constants
PHP constant names must be capitalized;
There should be no doubt about this. When I first started writing PHP, this writing method was already ingrained.
Method nameUse camel case (boyFriend)
The naming of methods is somewhat similar to the naming of classes, but there are still some differences: class The naming of methods requires the first letter to be capitalized (BoyFriendMoney), while the naming of methods requires the first letter to be lowercase (boyFriendMoney)
Recommended tutorial: "PHP
The above is the detailed content of What is PHP PSR?. For more information, please follow other related articles on the PHP Chinese website!
ACID vs BASE Database: Differences and when to use each.Mar 26, 2025 pm 04:19 PMThe article compares ACID and BASE database models, detailing their characteristics and appropriate use cases. ACID prioritizes data integrity and consistency, suitable for financial and e-commerce applications, while BASE focuses on availability and
PHP Secure File Uploads: Preventing file-related vulnerabilities.Mar 26, 2025 pm 04:18 PMThe article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.
PHP Input Validation: Best practices.Mar 26, 2025 pm 04:17 PMArticle discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.
PHP API Rate Limiting: Implementation strategies.Mar 26, 2025 pm 04:16 PMThe article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand
PHP Password Hashing: password_hash and password_verify.Mar 26, 2025 pm 04:15 PMThe article discusses the benefits of using password_hash and password_verify in PHP for securing passwords. The main argument is that these functions enhance password protection through automatic salt generation, strong hashing algorithms, and secur
OWASP Top 10 PHP: Describe and mitigate common vulnerabilities.Mar 26, 2025 pm 04:13 PMThe article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.
PHP XSS Prevention: How to protect against XSS.Mar 26, 2025 pm 04:12 PMThe article discusses strategies to prevent XSS attacks in PHP, focusing on input sanitization, output encoding, and using security-enhancing libraries and frameworks.
PHP Interface vs Abstract Class: When to use each.Mar 26, 2025 pm 04:11 PMThe article discusses the use of interfaces and abstract classes in PHP, focusing on when to use each. Interfaces define a contract without implementation, suitable for unrelated classes and multiple inheritance. Abstract classes provide common funct


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

SublimeText3 Linux new version
SublimeText3 Linux latest version






