How start php server with vs code?

Visual Studio Code is a great editor for PHP development. You get features like syntax highlighting and bracket matching, IntelliSense (code completion), and snippets out of the box and you can add more functionality through community-created VS Code extensions.

Linting

VS Code uses the official PHP linter (php -l) for PHP language diagnostics. This allows VS Code to stay current with PHP linter improvements.

Tip: Using XAMPP? Install the full version of PHP in order to obtain the development libraries.

There are three settings to control the PHP linter:

  • php.validate.enable: controls whether to enable PHP linting at all. Enabled by default.
  • php.validate.executablePath: points to the PHP executable on disk. Set this if the PHP executable is not on the system path.
  • php.validate.run: controls whether the validation is triggered on save (value: "onSave") or on type (value: "onType"). Default is on save.

To change the PHP settings, open your User or Workspace Settings (⌘, (Windows, Linux Ctrl+,)) and type 'php' to filter the list of available settings.

How start php server with vs code?

To set the PHP executable path, select the Edit in settings.json link under PHP > Validate: Executable Path, which will open your user settings.json file. Add the php.validate.executablePath setting with the path to your PHP installation:

Windows

{
  "php.validate.executablePath": "c:/php/php.exe"
}

Linux and macOS

{
  "php.validate.executablePath": "/usr/bin/php"
}

Snippets

Visual Studio Code includes a set of common snippets for PHP. To access these, hit ⌃Space (Windows, Linux Ctrl+Space) to get a context-specific list.

How start php server with vs code?

PHP extensions

There are many PHP language extensions available on the VS Code Marketplace and more are being created. You can search for PHP extensions from within VS Code in the Extensions view (⇧⌘X (Windows, Linux Ctrl+Shift+X)) then filter the extensions dropdown list by typing 'php'.

How start php server with vs code?

Disable built-in PHP support

To disable the built-in PHP smart completions in favor of suggestions from an installed PHP extension, uncheck PHP > Suggest: Basic, which sets php.suggest.basic to false in your settings.json file.

Debugging

PHP debugging with XDebug is supported through a PHP Debug extension. Follow the extension's instructions for configuring XDebug to work with VS Code.

Next steps

Read on to find out about:

  • Extension Marketplace - Browse the extensions others have shared
  • Debugging - Learn more about VS Code debugging

9/1/2022

How start php server with vs code?

How start php server with vs code?

Learn how to use Visual Studio Code tasks to build a PHP server without XAMPP or WAMP. In this tutorial, we will create 2 tasks in VS Code. One will create our PHP server in the terminal, the other will then launch Chrome and open our server. The end result is a quick and easy PHP dev environment that launches automatically with a quick key combination.

View This On YouTube

Launch a build task on Windows:

CTRL + SHIFT + B

Enter fullscreen mode Exit fullscreen mode

Launch a build task on Mac:

CMD + SHIFT + B

Enter fullscreen mode Exit fullscreen mode

Create A Task File in VSCode

When you are in VSCode, you can launch your tasks by using the key combinations above. It will either run your current build tasks or ask you to create a new one if you have not previously created one.

If you do not use the key combinations, you can go to Terminal > Run Build Task to go to the same place.

Click the "Configure Build Task" option that appears.

Once you are in your tasks.json you should see a default command that looks like this:

{
    "label": "echo",
    "type": "shell",
    "command": "echo Hello"
}

Enter fullscreen mode Exit fullscreen mode

This can be left in this file but you need to add the following in order to get your task for the PHP server to work properly.

PHP Server

In the same file, you can now add the following lines of code to create a simple PHP server. Make sure your json commands are comma separated. So each time you add a new task, add a comma after the previous task, like this:

{
    "label": "echo",
    "type": "shell",
    "command": "echo Hello"
},

Enter fullscreen mode Exit fullscreen mode

Now you can add your server task.

{
    "label": "php-server",
    "type": "shell",
    "command": "php -S localhost:8000",
    "group": {
        "kind": "build",
        "isDefault": true
    }
}

Enter fullscreen mode Exit fullscreen mode

You can test this by running your build tasks using the key combination or the Terminal menu option. This should start a local PHP server at the address localhost:8000.

Keep in mind, this tutorial allows you to render PHP code but does not come equipped with a database or some other services you would expect with something like WAMP or MAMP. This is to simply run your PHP code without an external service. You can still use your PHP code to hit external APIs but it does not spin up your own database.

Launch Chrome

To make this a little easier to work with while I am developing, I like to make it open my browser to the newly created server. You can do this by create a new task and pointing it to your Chrome (or other browser) .exe file.

Add the following as a task in your json file.

{
    "label": "launch-chrome",
    "type": "shell",
    "command": "chrome.exe http://localhost:8000/",
    "options": {
        "cwd": "C:\\Program Files (x86)\\Google\\Chrome\\Application"
    }
}

Enter fullscreen mode Exit fullscreen mode

Make sure your "cwd" is pointing to your browers's exe file. Mine currently resides in the following folder:
C:\Program Files (x86)\Google\Chrome\Application

Now you should have either 2 or 3 tasks in this json file. If you run the build tasks, it will not open the browser, this is because that task is not tied into anything else. We need to add the following code to your server build task to make them work together.

"dependsOn": [
    "launch-chrome"
]

Enter fullscreen mode Exit fullscreen mode

The server build task should now look like this:

{
    "label": "php-server",
    "type": "shell",
    "command": "php -S localhost:8000",
    "group": {
        "kind": "build",
        "isDefault": true
    },
    "dependsOn": [
        "launch-chrome"
    ]
},

Enter fullscreen mode Exit fullscreen mode

Now you can run your build task and it should start the server, then open Chrome if everything is setup as it should be.

Final Code

This is what my json looks like. If you are starting fresh, your should look like this as well. If you have tasks already, just make sure the previous code was added to the existing tasks and you select the build you want to run.

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "echo",
            "type": "shell",
            "command": "echo Hello"
        },
        {
            "label": "php-server",
            "type": "shell",
            "command": "php -S localhost:8000",
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "dependsOn": [
                "launch-chrome"
            ]
        },
        {
            "label": "launch-chrome",
            "type": "shell",
            "command": "chrome.exe http://localhost:8000/",
            "options": {
                "cwd": "C:\\Program Files (x86)\\Google\\Chrome\\Application"
            }
        },
    ]
}

Enter fullscreen mode Exit fullscreen mode

How run php server on VS Code?

There is a much easier way to run PHP, no configuration needed:.
Install the Code Runner Extension..
Open the PHP code file in Text Editor. use shortcut Ctrl+Alt+N. or press F1 and then select/type Run Code , or right click the Text Editor and then click Run Code in editor context menu..

How do you run a server with a VS Code?

Open VSCode Editor and Press ctrl+P , type ext install ritwickdey. liveserver ..
Open a project and directly click to Go Live from StatusBar to turn on/off the server..
Right click on a HTML file from Explorer Window & click to Open with Live Server . ... .
Open a HTML file and Right click on the editor and choose the options..

How do I start php code?

Step 1: First of all, open the Apache Friends website and download XAMPP for Windows, and install it. Step 2: Start the XAMPP Program Control Panel. Click on the “Start” button next to the “Apache” to start your Apache Web Server. Also, start “MySQL” if your PHP programs depend on a MySQL database to run.

How do I run php?

To run PHP for the web, you need to install a Web Server like Apache and you also need a database server like MySQL. There are various web servers for running PHP programs like WAMP & XAMPP. WAMP server is supported in windows and XAMP is supported in both Windows and Linux.