I'm trying to embed some php code in a .html file but it doesn't seem to work. I read that in order for this to work the file has to be saved with the .php extension, but when I do that and open it up in a browser the browser just displays all my code. What am I doing wrong?
<!DOCTYPE HTML>
<html>
<head>
<title>Welcome</title>
<link rel="stylesheet" href="normalize.css">
<link rel="stylesheet" href="style.css">
</head>
<body>
<?php
$x = 3;
$y = 2;
$ans = $x + $y;
?>
<h1 style = "text-align: center;"> The answer is: <?php= $ans ?> </h1>
<section class="loginform cf">
<form name="start page" action="index_submit" method="get" accept-charset="utf-8">
<label style = "text-align: center;" >What type of property?</label>
<input type="button" value = "Commercial" onClick="parent.location='commercial.html'">
<input type="button" value = "Residential" onClick="parent.location='residential.html'">
</form>
</section>
</body>
</html>
Your php code will not work with .html; Change file extension to .php. You should run your php code from the server. You can download AppServ or WAMP
Your browser doesn't understand PHP. What you need to do is upload your file to a web server that knows what to do with it. Most commercial web hosts are set up this way. Alternatively, you can set up a server on your own computer. If you search the web for LAMP + PHP (or perhaps WAMP if you're using Windows), you should find instructions on what to do next.
In the server, files with names ending in .php
are handled by a PHP server module, which looks for code between the <?php
and ?>
tags and executes it before sending the results on to the browser that requested the page.