Visual Code Php

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:

Code

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.

INSTRUCTIONS

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.

Set Breakpoint:

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.

Start Debugging:

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:-

  1. Check the connection to database.
  2. Create an html Form
  3. Insert data to database by filling form
  4. 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 .

db.php

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.

index.php

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).

Insert.php

Visual

DISPLAY DATA FROM DATABASE:-

To view data from database, we need to create a page

view.php

Install Php Visual Studio Code

EDIT DATA-

In order to edit data from database we need to click on edit button available on view.php.

Visual Code Php

edit.php

DELETE DATA-

In order to delete data from database using form we make page

delete.php

REGISTER.SQL

Code Formatting

The editor provides the document formatting feature for PHP documents. Right click the editor area and choose Format Document.

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 editor.formatOnType to true enables the editor to format code blocks and statements upon typing ; and }. This feature performs selection formatting of the code preceeding the type delimiter. It indents the code and pretty-prints spaces.

Configuration

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.

SettingEffect
editor.insertSpacesSpaces are used to indent code if true, tabs are used otherwise.
editor.tabSizeNumber of spaces that represent a single tab, this value is used only when editor.insertSpaces is true
editor.formatOnTypeEnables or disables automatic formatting of code block upon typing ; or }.
php.format.codeStylePreferred code style described in previous sections, available values are PHP Tools, PSR-2, or others described below.

Code Styles

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.

FeatureBehavior
Pretty printSpaces are inserted after keywords, names and separators to make the code more readable.
IndentationCode is indented based on the number of embedded blocks.
Multi-line commentsSubsequent lines are offset to match the first asterisk character on the first line
StatementsEvery statement starts on a new line.
Anonymous functionsThe body is indented from the beginning of the line where the function keyword is defined.
Use constructThe use construct in anonymous functions is always on the same line as the function keyword.
Control flowClose keywords of control flow constructs (endif, endfor, ...) always start on a new line.
NamespaceAn empty line is inserted after a namespace declaration.
Use statementUse 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.

FeatureBehavior
Multi-line expressionsSubsequent lines maintain their indentation from the first line.
PHP blocks indentationContents of PHP blocks is indented from the open tag (<?php or <?=).
Code blocksCode 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 .

FeatureBehavior
Multi-line expressionsSubsequent lines are indented one level deeper than the initial line
PHP blocks indentationContent of PHP blocks is indented from the beginning of the line, the open tag is not moved
Code blocksBody 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.