Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run PHP code from Visual Studio Code (VSCode)?

I can't find a way to run php on Visual studio code, Does anyone know how?

Duplicate:

Yes it is but a little bit different from here.

Steps:

I followed below steps to configure php in VS Code.

  1. Configure PHP linting in user settings enter image description here
  2. Install Php Debug extension in VSCode
  3. Then configure php.ini file enter image description here
  4. Create a external php file in root folder
  5. add <? echo "My First PHP site in VSCode."; ?> in external php file which I created now
  6. In my index.html file I referenced my php file like: enter image description here
  7. Run my web server apache using xampp control panel
  8. Build my project and run it on web browser it shows me nothing.
  9. Also when I open dev tools of my chrome browser its shows me my php code of index file commented. why? I don't know. enter image description here

Question:

What I am doing wrong in my above steps to configure php in vs code. Please explain me in easy steps so I can easily achieve my goal. Thanks in advance.

like image 450
Ahmer Ali Ahsan Avatar asked Aug 21 '16 19:08

Ahmer Ali Ahsan


People also ask

How do I run Visual Studio Code in VS Code?

To run or debug a simple app in VS Code, select Run and Debug on the Debug start view or press F5 and VS Code will try to run your currently active file.

How do I run a PHP file?

php” file is placed inside the “htdocs” folder. If you want to run it, open any web browser and enter “localhost/demo. php” and press enter. Your program will run.


1 Answers

Looks like you in fact don't want to run PHP from Visual Code, but instead you're trying to get PHP to work at all.

  1. add in external php file which I created now

You're using short tags and that's ok, if your configuration allows it, however I would recommend using explicit PHP tags: <?php echo "My First PHP site in VSCode."; ?>

In my index.html file I referenced my php file like:

There's the problem. You're placing PHP code in a HTML file. PHP code in HTML files won't be (at least by default) executed. Change the filename from index.html to index.php.

That should do it.

like image 118
Smuuf Avatar answered Oct 06 '22 04:10

Smuuf