Some of the most followed PHP Standards and Coding-styles

Search for a command to run...

No comments yet. Be the first to comment.
Image Generated using Midjourney I recently published this article, where I am talking about some MidJourney tricks and tips after which I got some messages saying what I Midjourney. I know my bad, I did not talk about Midjourney a lot in that articl...

Most of these tricks work with BlueWillow, Stable Diffusion, and many other models. Image generated using BlueWillow Midjourney is all over the rage right now, people are going crazy over it and trying different prompts to create beautiful artwork bu...

Photo by Towfiqu barbhuiya on Unsplash This is part 3 of the Managing your Finances article series, if you have not read part 1 and part 2, then I would recommend you to read those before starting with this article. You have started the journey and y...

Photo by micheile henderson on Unsplash This is part 2 of the Managing your Finances article series, if you have not read part 1, then you can read it here, after which you can resume this article. If you are sorted with your current financial situat...

Photo by Mathieu Stern on Unsplash Before starting the journey of Financial Freedom or even improving our finances, we should be prepared by assessing our current financial situation to avoid bumps during our journey. Think of this preparation as col...

Photo by Christopher Gower on Unsplash
PHP has PSR (PHP Standards Recommendations) which has an extensive list of standards but most developers follow some common standards which increase code readability and maintainability. In this article, I am going to share some of these most-followed standards by PHP Developers.
This standard suggests that if a PHP file has a class declaration then it should not have any other statements in the same file.
You must have encountered a composer auto-loaded helper file that has some common functions used through-out the application. It is recommended that such a helper file should have only function declarations and nothing else. It is also recommended that these function names should be in `snake_case` and not in `camelCase`.


else block:When one starts learning programming and control structure, if__else is one of the first control structure that we learn but it is recommended to completely avoid the else block in your code since you never need an else block, instead of using else block you can consider simplifying the code block.

Most developers recommend using custom Exception classes extending from \Exception class of PHP with a custom error message.

It is recommended to use User-defined constants instead of using hard-coded values. It increases the readability of the code.

Most developers prefer using the NOT (!) operator for NULL checks or empty checks.

When it comes to code formatting standards, there are various editor extensions available which does it for you, for PHP, I use PHPFmt with Visual Studio code which formats and aligns the code for better readability.
You can also install PHP Mess-detector with Visual Studio code which will give you warnings for Code mess.
Note: Almost all the points mentioned above are just recommendations and not mandatory.