visual studio does not recognize php - 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)

Related

PhpStorm runs only JavaScript instead of PHP?

I have just installed fresh PhpStorm on Mac. I set my interpreter and so on. I can create .php files. But when I run it runs only JavaScript and there is no option to run php? How can I fix this?
Run 'filename.php (JavaScript...)'
Same with debug. How this can be fixed?
It seems you've configured PhpStorm to handle files with .php extension as JavaScript code rather than PHP.
Open the settings dialog (File/ Settings), head to Editor/ File Types and review the Recognized File Types section:
Remove *.php from JavaScript.
Make sure that PHP contains *.php.
Remove any other unwanted registered pattern, such as filename.php.

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

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

PHP include format for Kirtan Central website

I'm trying to use a PHP include for the first time. I'm reasonably familiar with HTML, and new to PHP.
I've put the HTML for my header in a separate PHP file, and am trying to call it from http://www.kirtancentral.com/index-test.html. You can see I attempted it in a bunch of formats, I was of course hoping this one would do the trick:
<?php include("/home/danielctucker/kirtancentral.com/includes/header.php"); ?>
Not sure what I'm doing wrong!
You have to make your web server execute the PHP before any PHP embedded in a page will work. The fact you can see the PHP when you View Source shows that this is not happening.
Most servers (which have PHP installed and enabled) will only look in files with a .php file extension for PHP code (although this can be configured otherwise).
Try changing from index.html to index.php. If that doesn't work, then you need to install and enable PHP (if you control the server) or change your hosting pacakage.
You do not have PHP enabled on your server.
Install/enable PHP
Also it is good practice to use a relative path to your PHP script:
<?php include("includes/header.php"); ?>
Of course, this will only work when you have PHP enabled on your server.
Radhe-Shyam! :)

Including different types of files in PHP

I took over a PHP project and I'm trying to get it running on my dev box. I'm a developer and not a sysadmin, so I'm having trouble getting it working.
The previous developer used .h files to include PHP. My current configuration is taking that .h file and including it without executing it. What do I need to look for in my apache config (or is it my php.ini)?
EDIT:
Something clicked when I read one of the comments below. The code is using ASP style tags "<?". I've turned the option on in php.ini and according to phpinfo(), it's enabled, but apache is still just including the code as text.
I just checked it and running the code with the full opening PHP tag "<?php" fixes the problem. Having said that, I'd still like it to work the other way.
I'm running the code on a Macbook with the latest versions available. PHP 5.2.6, postgresql 8.3 and apache 2.
The code works on the staging server, but I can't figure out what the difference it.
EDIT
Durrr...I didn't have short_open_tags enabled in php.ini.
Could the problem be that the .h file you are including just starts into php code without the opening "<?php" tag?
From include documentation:
When a file is included, parsing drops out of PHP mode and into HTML mode at the beginning of the target file, and resumes again at the end. For this reason, any code inside the target file which should be executed as PHP code must be enclosed within valid PHP start and end tags.
PHP is an interpreted language, so you can't 'include' a file without executing it. Remember that '.h' is just a file extension, so although the previous coder may have put PHP in a '.h' file, it's still just PHP.
More details on the problems you're running into would be helpful.
you can use a .htaccess file and add:
php_value auto_prepend_file /path/to/include-file.h
Edit
I've just read that .htaccess only works with the module version of php.
You should change them all to .php extensions. But, if you are going to leave them as .h, you change the mapping in Apache. It's Apache that runs the file through the proper interpreter, not PHP. The PHP engine will process anything passed to it.
include and require will execute the included file. If you are using readfile, it simply echoes the file without treating it as PHP code.
If the .h files are "missing" the <?php directive, then you may have to switch from include to something like:
eval( file_get_contents("file.h") );
Personally I try to avoid eval whenever I can, but sometimes there is just no choice.

Categories