Running external php file from HTML page

Hi everyone
This is my “hello.php” file:

	$myConnection = NEW MYSQLI('localhost','root','a','b');
	echo 'Hello world';

When I load it thorough my browser typing at the address bar:
http://localhost/hello.php I get an “Hello world” declaration on my display.
Then I create an hello.HTML file with the following code:

	<a href="hello.php">php</a>				

when I load “hello.html” and click “php” anchor, what I get at my display is the php file’s source code.
I dont get the desired “Hello”.
Could anyone tell me why and what should I change to make the browser show “Hello” and not the source code?

You’re not running the php file from an html page, you’re visiting a php file from an anchor in the html page.

It worked for me on my local development, I copied what you provided.

I’m guessing it’s a php config setting.

I suspect what you’re doing is opening the HTML file from your file system, perhaps by just browsing through file manager for it. What you need to do is the same as you did for the php file, in your browser address bar type in


which will call the local WAMP/XAMP server to display the page and process the HTML. One way to check is when you open the html file, what does the browser address bar contain? If it starts with “file://” then it’s opened via the filesystem, if it starts with “http://” then it’s opened via the server.


That is so. Thank you very much !

I have the same question, anyone please help

The question has already been answered, @nguyenngoc22789.

What do you not understand in the explanation given?

1 Like

It is not the same if the answer is different. One possibility is that the server might not be configured to process php files using PHP. Neither you nor deotpit specify what server software you are using for your local server. I am nearly certain that for IIS we must modify the configuration to specify use of PHP.

True, though in the case of the OP they had said that if they run the php code directly from the server, it works and produces the output they expected, hence my not considering the possibility that it might be a configuration thing.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.