0

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>

            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <input type="button" value = "Commercial" onClick="parent.location='commercial.html'">

            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <input type="button" value = "Residential" onClick="parent.location='residential.html'">

        </form>     
    </section>
</body>
</html>

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Even the search XAMPP will do ;) And it's not the browser that has to understand what PHP is, it's the web server.
@dbf Precisely. So you need a server that knows what PHP is. (Wait a minute, didn't I just say that..?)
Okaaay.... are you trying to tell me there's something wrong with that statement?
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.