Debugging PHP with Visual Studio Code
Installing PHP with PHP Tools for Visual Studio. PHP is required to run your PHP project locally. Besides, the PHP's Xdebug extension is required as well, in order to get the debugging functionality working. Visual Studio Code is a code editor redefined and optimized for building and debugging modern web and cloud applications. Visual Studio Code is free and available on your favorite platform - Linux, macOS, and Windows.
Visual Code For Windows
Debugging is one of the most important thing of our application development. To make quality software, debugging is a vital part of development.In this post, I am going to show how to debug PHP with visual studio code.
About Visual Studio Code:
Visual studio code, developed by microsoft is an open source code editor features integrated debugging for almost all programming language.
Installation of VS Code:
To get started first visit https://code.visualstudio.com to download visual studio code. Once VS code installed open it and then you need to install PHP debug extension.
Installation of Xdebug PHP Extension in VS Code:
To install PHP Extension just move to the extension in sidebar icons of VS code or you can press CTRL+SHIFT+X to explore extension. After that type PHP debug in search extension and install it.
After successfully installed Extension you need to setup user setting. Just Go to setting or press CTRL+, you will be move to the setting and change executable path as i am showing below.
Installation of Xdebug:
To configure xdebug in local server you need to go https://xdebug.org/wizard.php and paste here your phpinfo(). After that you will get instructions what to do next.To get phpinfo, go to the browser and type localhost you will be move to the dasboard and here you can find the phpinfo menu just click it and copy all the content by pressing CTRL+A.
Debugging Setup in VS COde:
Next we need to setup debugging mode in vs code. Just open folder containing PHP Code file and choose the debug mode from the left hand side.
To debug, need to add breakpoint in code. To add breakpoint click on the left of the line where you want to add breakpoint as i am showing showing below.
Breakpoint are indicated by red dot. When execution start on browser, it will stop at breakpoint where we can check the state of the code and variables.
To start debugging just click on the start debugging option. For the first time it will create configuration file, no need to change for our puposes .If everything is setup correctly then VS code will be in debbuging mode. Run your page and you will get your debugging environment in VS code with Xdebug.
Thank you for reading this post. we hope you like this Post, Please feel free to comment below, your suggestion. If you face any issue with this code let us know. We’d love to help!
Insert, Edit, Update, Delete Data using Php
In order to insert, edit, update Data in Php, We need to follow certain steps:-
- Check the connection to database.
- Create an html Form
- Insert data to database by filling form
- Edit, Update, Delete Data.
CHECK THE CONNECTION TO DATABASE.
In order to check connection to database, we need to know SERVER NAME, USER, PASSWORD, DATABASE .
NOTE:- While working on local computer server name is localhost, username is root,and password is blank (by default).
CREATE AN HTML FORM:-
In this step we simply use html to create a form.
Here action=”insert.php” means when we click on submit button the page will redirect to insert.php page. Method=”post” means when form will submit form will use post data.(Post data is referred most times because post method can carry maximum data from one page to another).
DISPLAY DATA FROM DATABASE:-
To view data from database, we need to create a page
Install Php Visual Studio Code
In order to edit data from database we need to click on edit button available on view.php.
In order to delete data from database using form we make page
The editor provides the document formatting feature for PHP documents. Right click the editor area and choose
In order to format the document, it must be syntax-error free.
The code formatting normalizes whitespaces, line endings, opening and closing braces, indentation and pretty print spaces.
Format On Type
Setting the option
true enables the editor to format code blocks and statements upon typing
}. This feature performs selection formatting of the code preceeding the type delimiter. It indents the code and pretty-prints spaces.
Visual Code Php Example
The formatting behavior is set in
settings.json. All the relevant settings and their effects are summed up in the following table.
|Spaces are used to indent code if |
|Number of spaces that represent a single tab, this value is used only when |
|Enables or disables automatic formatting of code block upon typing |
|Preferred code style described in previous sections, available values are |
Document formatting is available in two versions that differ in the way they structure the document: PHP Tools, PSR-2. The styles are selected in settings. The styles share most features, summed up in the following table, while they differ in a couple of details, explained in their own sections.
|Pretty print||Spaces are inserted after keywords, names and separators to make the code more readable.|
|Indentation||Code is indented based on the number of embedded blocks.|
|Multi-line comments||Subsequent lines are offset to match the first asterisk character on the first line|
|Statements||Every statement starts on a new line.|
|Anonymous functions||The body is indented from the beginning of the line where the |
|Use construct||The use construct in anonymous functions is always on the same line as the |
|Control flow||Close keywords of control flow constructs (|
|Namespace||An empty line is inserted after a namespace declaration.|
|Use statement||Use declarations are not separated by empty lines, but an empty line is inserted after all the declarations.|
Code Style: PHP Tools
The default code style is compliant with the formatting in PHP Tools for Visual Studio. This style focuses on leaving the code visual structure as untouched as possible, while normalizing the whitespaces where possible.
|Multi-line expressions||Subsequent lines maintain their indentation from the first line.|
|PHP blocks indentation||Contents of PHP blocks is indented from the open tag (|
|Code blocks||Code blocks always start on a new line for both declarations and code.|
Code Style: PSR-2
This formatting style is compliant with PSR-1 and PSR-2 standards .
|Multi-line expressions||Subsequent lines are indented one level deeper than the initial line|
|PHP blocks indentation||Content of PHP blocks is indented from the beginning of the line, the open tag is not moved|
|Code blocks||Body of a declarations starts on a new line, while control flow block starts on the same line as the construct|
Vs Code Php Setup
Code Style: Off
To turn off the formatting select the setting
Off. Remaining feaytures like PHPDoc auto-generation will work but Document Format, Selection Format, Format On Paste and Format On Type will do nothing.
Visual Code Php
HTML, JS and CSS Formatting
PHP Tools support HTML, JS and CSS formatting in PHP files. The HTML fomatting process is provided by Visual Studio Code and PHP Tools integrates the functionality with PHP formatting.