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

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.
Configure PHP linting in user settings
Install Php Debug extension in VSCode
Then configure php.ini file
Create a external php file in root folder
add <? echo "My First PHP site in VSCode."; ?> in external php file which I created now
In my index.html file I referenced my php file like:
Run my web server apache using xampp control panel
Build my project and run it on web browser it shows me nothing.
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.
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.

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

Related

PHP echo: can't reproduce W3school's code [duplicate]

This question already has answers here:
PHP code is not being executed, but the code shows in the browser source code
(35 answers)
Closed 6 years ago.
I just started learning php and this is what I have done:
I copied the code from the left panel on this page and pasted them into a local html file. But when I open the html in browsers (firefox and chrome), the page doesn't look like what it should be: There is no format and the word 'echo' and some symbols are shown on the page. Why does this happen? How can I fix this issue?
PHP is a serverside scripting language. It has to be interpreted by a PHP interpreter that is in some way connected or integrated into an HTTP (web) server. There are a number of different ways to do that, with varying degrees of complexity.
How and where you setup your php + webserver is up to you, and the ways to do that typically start with the operating system you are running your workstation on, assuming you want to do so, locally.
You can go right to here for a jumpstart: http://www.phptherightway.com/#getting_started
The extension for a page that has php codes must be .php and not .html. Also, for the PHP code to execute you must be running Apache server on your machine.
This is a good tutorial for beginners, which tells you how to run PHP codes.
The steps are as follows
Open any Text Editor. Install new if you don't already have any good Text Editor installed. (My Favorite is Sublime Text & Notepad++)
Write the following PHP Program / CODE in the Text Editor: '; ?> This is
test.php File.
Save the file in XAMPP Installation Directory \ Web Root Directory Note-1: Default XAMPP Installation Directory in Windows is C:\xampp
Note-2: Default Web Root Directory in XAMPP is htdocs. All your php
files will have to be in this htdocs folder. That means, for a typical
installation of XAMPP in Windows, you will have to save the PHP CODE
in C:\xampp\htdocs folder.
When you save the file, name it test.php (just as an example, any valid file name with .php in the end will work). Note: when you save
this file, make sure it has no .txt extension at the end. Some text
editors place .txt at the end of file name, so it becomes test.php.txt
instead of test.php. To avoid this, when you save the file using any
text editor, place double quote around the file name: e.g. "test.php"
Then, go to XAMPP installation folder (typically, C:\xampp) and run xampp-control.exe by double clicking it.
In the xampp-control window, click the start button beside Apache. Later, if you need other options like MySQL, you'll also have to start
MySQL by clicking the start button beside MySQL in the XAMPP control
Panel. Note: if your OS hides common file extensions, then you'll see
xampp-control, instead of xampp-control.exe
Now, in your web browser's address bar, type the address: http://localhost/test.php

visual studio does not recognize php

i added php code in my cshtml view under asp.net mvc.
<body>
<?php echo"hello";?>
...
however it seems that vs does not understand that it's php and when I inspect element, the php code was automatically commented out.
<!--?php echo"hello";?-->
how can I solve this? I have already installed php tool extension in vs.
To run php script, you need to have .php files, or it won't be recognized.
To enable the script to be recognized as php without changing file suffix, add config files depending on the server you are using. (most likely Apache/IIS)

PHP in html not parsing

I have a html file and I want to run PHP code to call out an array value within HTML form box but the interpreter doesn't recognize
<input value="<?php echo $_SESSION['user']; ?>"/>
p.s. Not sure if it makes any difference but I am running this on cloud9 with apache (httpd).
Save your HTML file as a PHP file since HTML files cannot execute php code. For example if your file is named index.html you want to re-save it as index.php once you do that your php code should run.
HTML files does not parse PHP. You need to have a file with .php as extension to run PHP.
PS: Its possible to make HTML file run PHP as well but that requires some extra settings in apache config which is never enabled by default. Reason being a security threat. But If you have a dedicated server which allows playing with apache config then you can achieve this..
have a look at this LINK
Ok I figured out that it was actually working all along =/ (i was only looking at the local IDE display within cloud9 but the code was working all along since it actually required back-end processing and the local IDE won't display that).

Code shown as text output

I'm a newbie to Facebook app development. It almost works when the page liked.php is shown (it's shown when a user liked the site), the PHP code is displayed as text output.
my liked.php code:
deleted for privacy reasons
?>
Here's the output(it's shown as blank text):
I simply don't know why. Even the error_reporting(E_ALL); doesn't show anything. Simply blank text output. I use the newest facebook php sdk.
I guess the problem is not in the code but in the configuration of your server. You need to tell the server to execute the php code instead of displaying it.
Sounds like you either don't have PHP installed or you're not using a file extension which invokes the PHP engine - is your file called [Blah].php?
Some things to check:
You're serving the file from a web server (not locally)
The server has PHP installed
Your webserver is configured to use the PHP engine to handle the file type you're using (usually .php or .php4 or similar)
creating a new file with just <?php phpinfo(); ?> and serving it should give you lots of info about your PHP install.
If you have access to the server, you should be able to run PHP from the command line/shell

Call to PHP prints source, doesn't run when called from another file

I have a quick question that I can't figure out. I've tried searching Google and following examples, but I can't find anything.
I have an HTML form that I'm trying to process with a PHP file, but when I submit the form, it merely prints the source of the PHP file, it doesn't execute it. If I run the PHP file by itself (not indirectly through the HTML button), it works fine.
HTML form header:
<form id="registrationform" name="registrationform" method="post" action="processregistration.php">
Submit button:
<button type="submit" value="Submit" >Create</button>
The PHP form is just <?php print "Hello"; /?
Again, it runs fine if I just run the PHP file, but prints the PHP file (doesn't run) when it gets called through the HTML form.
Any help is appreciated.
edit-Running locally through Coda
edit-Here is the output that I'm getting:
Output when the PHP is called through an HTML action:
</php
print "Hello";
?>
Output when I run the PHP directly through Coda:
Hello
I had the same problem and just fixed it on my coda version 1.7.4
I installed MAMP on my mac and set up the apache path under MAMP preferences to the folder where my sites are located;
/Users/yourUserName/Sites
this points MAMP's 'http://localhost:8888' address to your sites folder, if you paste that on your browser you'll now see your sites folder's content in the browser.
then, all you do is point the local site's connection settings to the site you are testing;
I was going to post an image, but I'm new to the site and wasn't allowed.
Under my site's preferences i set up the addresses as follows;
Root URL : 'http://localhost:8888/yourSiteRootFolder/'
Local URL : 'http://localhost:8888/yourSiteRootFolder/'
Remote Root : /yourSiteRootFolder/
Local Root : /Users/amartinez/Sites/yourSiteRootFolder/
I hope this is related to the problem you're having and helps you fix it.
Line #19 from Coda 1.6 Release notes:
Coda no longer tries to locally
preview a remote PHP file while
editing/previewing
Listed under "Improvement" - doubt they brought it back for the 1.7 release.
I'm running Apache2/PHP5 10.5.8 OS X (no problem reproducing your issue with Coda)
Even when running my form.html and post.php files from /Library/WebServer/Documents folder.
My sample files work fine in the Apache env....I just needed to run them through Coda to "break" them. :-)
Are you sure the html file and the php script are on a server with php support enabled?
I've never used Coda, but you need to be running it through a server (e.g. Apache + PHP). You cannot just open the file itself within Windows.
Try looking at xampp as a quick server for testing.
If it works properly, you should be viewing the PHP file on something like http://localhost/test.php instead of file:///something/test.php.
This may be because you don't have PHP installed on your server. I would check to make sure that your hosting package included PHP, pre installed.
Here are some resources to get you started with that, if not installed:
http://www.thesitewizard.com/php/install-php-5-apache-windows.shtml
http://www.php.net/manual/en/install.php
http://www.w3schools.com/PHP/php_install.asp
Edit: actually, I think I may have found it.
If you look at the starting PHP tag, you have a slash instead of a ?. If that's in your script, just change that to <?php. But it may not be, it could be that's just question format-ing.

Categories