Introduction to PHP formatter
In every programming language, we have a code structure that we need to follow, which maintains the readability of the code to other developers. This is something called coding standard in general. Like other programming languages, we have many tools available online where we can format our code by applying some styles to them. Formatter is nothing but used to format our code, making it easy to understand and readable by the new programmers by following the coding standard of language. In this topic, we are going to learn about PHP formatter.
How does PHP formatter work?
As we know, we know that formatter is used to format our code, and online we can find many formatters. We can either use a visual studio for this or any online formatter which provides good support for PHP. First, we will see how we can format our code using visual studio and what setting do we need to make this.
Visual Studio
Visual Studio provides good support for PHP where we can format our code by applying some style, but for this, we need to make some settings into it. By using it, we can perform bracket matching of methods or classes; we can also highlight syntax, remove extra space, and align the code where needed. In addition, it will force us to follow the proper indentation and make our code look more beautified and presentable to others.
The visual studio uses the PHP linter to provide the improvement. But to use this, we have three different kinds of settings to control the liner of PHP :
- php.validate.run: This setting will trigger the validation on the save of any file inside Visual Studio. The means once we save the file, then it will start validating. We can disable this setting as well, but its default value is onSave only. It can trigger validation on typing od code or on saving of the file. In most of the editors, it is typed only.
- php.validate.enable: This key we can say enables the linter, and if we check, this is enabled by default in the editor.
- php.validate.executable.path: This key checks that wheatear the executable of PHP is on disk or not. It keeps on monitoring that.
In visual studio, provide some of the available snippets for PHP to check this; we have a short-cut key ctrl+space like any other editor where we can see the available options.
Like the visual studio, we have many other formatters available for PHP. Now we will talk about the online formatted, which can be easily used just by copy-paste the code.
http://www.phpformatter.com/ – This is the online available formatter for better understanding. I am attaching a screenshot of this formatter. See below;
image :
As we can see here, this formatter provides us with three options here to format our code based on various parameters. Let’s discuss each of them in detail:
1. input: This is the input parameter that takes the input and gives us some output. This input basically our code which we want to format according to a coding standard. Like it will remove unnecessary spaces, unused packages, if any, and many more options available. So in the section, we will just be going to copy-paste the code that we want to format.
2. Style: As the name suggests, it gives, or we can say, add style to our code or file and arrange its proper format. In this section, we can select different types of styles for our code. Like see below ;
- Indentation style: There are different types of style available some of them are mentioned below;
K&R style (One true brace style) | Allman &style (BSD style) | Whitesmiths style |
GNU-style | PEAR style | NA |
- Starting Indentation: In this block, we can give an integer value.
- Indentation: In this block, we can give value as an integer by default for PEAR style; it is 4.
Apart from this, we can perform many things like on brackets, switch operations, functions, highlight keywords, and many more.
3. Output: This is the last tab that is available, and it basically shows us the output. It will contain all the styles that we have applied for a particular piece of code while doing the formatting of it. So from here, we can copy our code and place this into our workspace without any additional settings.
We can download some extensions from the visual studio marketplace to format our code as needed. We can also use one short key for this (ctrl + shift + X); this will give the list of all the available extensions. We just need to filter them out based on our key. For example, just type in ‘PHP’, and you will see a list of all extensions and add them to your workspace.
The list of extensions available in the visual studio is as follows to format our code :
Code runner | Prettier | HTML CSS support |
PHP IntelliSense |
Examples of PHP formatter
Given below are the examples of PHP formatter:
Example #1
In this example, we are using the PEAR style to format our code. In the output, you will some highlighted code as well with proper space in between.
Code:
<!DOCTYPE html>
<html>
<body>
<h4>demo for formatting PHP code </h4>
<?php
echo "Demo for PHP formatter!!";
echo "first line!!";
echo "second line!!";
?>
</body>
</html>
Output:
Example #2
In this example, we are using different styles to format the code. Also, we are removing all the comments from the code. So in the actual program, you will see the comment, but you will not see the comment in the formatted program. This is very quick.
Code:
<!DOCTYPE html>
<html>
<body>
<h4>demo for formatting PHP code </h4>
<?php
//commenting code here
echo "demo to use different style in code formatter !!";
?>
</body>
</html>
Output:
Uses of PHP formatter
- Formatter very much needs to understand the code.
- It makes our code very much readable.
- We can highlight the main keywords by using this so that it will increase visibility as well.
- We can remove unnecessary comments and spaces from the program. We do not need to do this for each file and remove each line; it will automatically do this for us.
- We can also remove the empty lines from the code.
Conclusion
In PHP, we have many formatter available; also extensions from the visual studio are there. If you want to follow an easy-going approach, then we can use any online formatter by just copy-pasting the code. It makes our code looks very beautiful and professional.
Recommended Articles
This is a guide to PHP formatter. Here we discuss the introduction and working of PHP formatter along with examples and code implementation. You may also have a look at the following articles to learn more –
5 Online Courses | 3 Hands-on Project | 28+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses