Understanding PHP Standard Recommendation (PSR)

Share
image

PSRs is a PHP specification published by the PHP Framework Interop Group. It serves the standardization of programming concepts in PHP. The aim is to enable interoperability among components like frameworks, libraries, & projects.

Understanding of PHP Standard Recommendation (PSR):

The PSRs encompass different facets of PHP development, covering coding style, autoloading, coding standards, and HTTP message interfaces. Among the frequently cited PSRs are PSR-1, PSR-2, PSR-4, PSR-7, and PSR-12.

Step By Step Guide PHP Standard Recommendation (PSR)

Step 1: Create a folder and set up a PHP file using the .php extension

Step 1: Create a folder and set up a PHP file using the .php extension

Step 2: Implement the code inside the PHP file without the code-sniffer extension

PSR.php

// Invalid way to write the Coding standards.

<?php
$a = 20;
$b = 10;
if($a > $b)
{
echo “a is greater than b”;
}

Step 3: Install phpcs extension for vs code

Step 3: Install phpcs extension for vs code

Step 4:Further, Download the php_codesniffer by setting the composer section

Before using php_codesniffer, let’s establish a Composer,

For Ubuntu use this ref:

How to install composer

After installing composer, Then, in your terminal, execute the following command:-

After Hitting gedit~/.profile command, instantly open your .profile page & you can additionally add the below lines in the last:

# Set PHPCS path in profile
export PATH=~/.config/composer/vendor/bin:$PATH

Step 5:Go to VS Code setting->Search (phpcs)->Enter the executable path manually for phpcs according to your project directory

 Go to VS Code setting

Let’s Wrap It Up!

Now you can check your file, show errors of Coding Standards
And, here you will even get to know where you are making mistakes & why the code is facing issues while getting executed. Through this, you will be able to rectify them to let the code work smoothly.
At LN Webworks, our group of experts in Drupal are always prepared to help you make it happen. Get in touch with us right now to schedule a free consultation.

LN Webworks
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.