7

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.

Community
  • 1
  • 1
Ahmer Ali Ahsan
  • 5,636
  • 16
  • 44
  • 81
  • If your problem is getting php files to work on a localhost on Windows just follow the MS instructions [Install IIS and PHP](https://learn.microsoft.com/en-us/iis/application-frameworks/scenario-build-a-php-website-on-iis/configuring-step-1-install-iis-and-php). It's simple and Windows takes care of handling the install without you having to find downloads. I followed the part that uses Turn Windows features on or off. – aamarks Dec 23 '19 at 20:53

1 Answers1

5

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.

Smuuf
  • 6,339
  • 3
  • 23
  • 35
  • I appreciate your reply. Second thing what I am doing wrong is as you can say that I put above code in my html file. Yes Exactly what I did it. when I changed my file name from .html to .php and put my php (project folder) inside htdocs then finally my php website run. 3rd thing if I run my php code from .html file I search it on net and got a solution by creating an file ".htaccess" and put these code inside it `AddType application/x-httpd-php .htm .html` so my website and server side code easily be access from html file. Again thanks for your answer. – Ahmer Ali Ahsan Aug 24 '16 at 00:36
  • 2
    No problem, I'm glad I could help. However I would discourage you from using `AddType` directive to tell Apache to run your `html` files as PHP. It's good to have them separated. Just use `.php` extension for dynamic pages and `.html` for static ones. At adds security from unwanted code execution and it also saves your server a little bit of performance, because it won't have to run **every** page through PHP processor. – Smuuf Aug 24 '16 at 10:39