PHP script doesn't show up!

Hello everyone,
On me desktop I installed APACHE server and activated it as shown on the following screenshot.

Then I wrote an HTML file that says:


<!DOCTYPE html>
<html lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>test</title>
	</head>
<body>
aa
<?php
echo "Hello World!";
?>
</body>
</html>

Then, on my Chrome parser, at the address line I wrote the following address:

http://localhost/test.html

On my display appeared “aa” and nothing more! Whre is “Hello world”?! Why does’nt php script shows up?!
Thanks!

As far as I know, an HTML file cannot call PHP functions. If you want the PHP part to work, you must rename the file to .php. All HTML will work in .php files. I believe you can change file extensions using Apache and change .php to .html in the URL so people will think that the file is .html instead of .php.

Yes, the page needs a .php extension to tell the server to process the PHP in it. You can also change the server settings to ask it to do this even on .html pages. Also be aware that installing Apache is not enough. You also need to make sure PHP is installed, and to make this easier for yourself, it’s worth installing a program that has all this set up for you, like MAMP or XAMPP.

EDIT: O, I see that you are already using XAMPP, so just focus on the file extension. :slight_smile:

Thanks a lot. It seems I need to change extension.

Thank you Ralph. I’ll change extension hoping new 100 problems will not emerge instead…

XAMPP should not pose too many problems. I would recommend running XAMPP with administrator rights. XAMPP likes to run with administrator rights. This is what my XAMPP output says when I first start it:

1:11:26 PM  [main] 	XAMPP Version: 1.8.3
1:11:26 PM  [main] 	Control Panel Version: 3.2.1  [ Compiled: May 7th 2013 ]
[B]1:11:26 PM  [main] 	Running with Administrator rights - good![/B]
1:11:26 PM  [main] 	XAMPP Installation Directory: "c:\\xampp\\"
1:11:26 PM  [main] 	Checking for prerequisites
1:11:27 PM  [main] 	All prerequisites found
1:11:27 PM  [main] 	Initializing Modules
1:11:27 PM  [main] 	Starting Check-Timer
1:11:27 PM  [main] 	Control Panel Ready

https://community.apachefriends.org/f/viewtopic.php?t=67072

Thank you !
I just wondered what is the advantage of loading it with administrator’s rights. Will that enable me running PHP from HTML file?

By default, PHP only executes in files with a .php extension. PHP will be ignored in files with any other extension. If you want to execute PHP code embedded in a file with a .html extension, you will have to add a handler to your httpd.conf file (or in your virtual host file if you use one) to instruct Apache to process .html or .htm files as PHP. Is that what you want?


AddHandler application/x-httpd-php .php .html .htm

Putting that line in your httpd.conf file or in your virtual hosts file (if you use one) will instruct the web server to parse files with .php, .html, and .htm file extensions as PHP. That means that files with .html extensions containing no PHP and only straight HTML will be processed by the parser as well.

XAMPP likes to operate with administrator rights. That’s why I mentioned it.

Thanks again,
httpd.conf in my platform can be changed but my hosting machine, I guess, wouldn’t let me do that so I’ll stick to “.php” files till I reach a dead end like I always do :wink: then I’ll go back using “.html”.

Most likely, but there is a simple alternative, which is to place similar code in a .htaccess file:

AddType application/x-httpd-php .html .htm

I’ll stick to “.php” files till I reach a dead end like I always do

You won’t have a problem using the .php extension. Any time you are using PHP, stick with that extension. And even if you aren’t using PHP, it’s OK to use, and will save you grief later on if you need to run some PHP on the page. When I do a static site, all of the pages are .php, because I use includes for things like header and footer etc.

Thank you so much Ralph,
I’ll stick to PHP extension and that will save me using .htaccess for the time being. In future, I belive, I’ll use that file for different goals.
Thanks again