jo phul
jo phul

Reputation: 647

extraneous output from hello world php html javascript page

I am a noob with web based languages, and was just trying to figure out the server/client side of things.

I have this index.html file stored in two locations.

1) c:\MAMP\htdocs\Test\index.html

2) c:\Test\index.html

<!DOCTYPE HTML>
<html>
    <body>
        <p>Hello from html!</p>
        <script>
            document.write( '<p>Hello from javascript!</p>' );
        </script>
        <?php 
            echo "<p>Hello from php!</p>"; 
        ?>
    </body>
</html>

What confuses me, is that the output is

Hello from html!

Hello from javascript!

Hello from php!
"; ?>

I don't understand why I get the trailing "; ?>

I have Apache installed (via MAMP), and I was expecting that if I open the index.html file via the server in my web browser http://localhost/Test/index.html the php would execute, and if I open it in the web broswer directly with file:///C:/Test/index.html the php wouldn't execute. But I get the same output either way.

Upvotes: 0

Views: 36

Answers (2)

Doug
Doug

Reputation: 6487

Issue is that the PHP is not being executed on both cases.

When you have a look at the HTML code generated is easy to see that the php was not executed at all (this is me simply opening the file not running any MAMP/XAMP):

enter image description here

To solve this problem try to change the extension to .php and to make sure you have MAMP running.

Upvotes: 1

ChrisD
ChrisD

Reputation: 147

The server wont parse PHP when it's inside a file with a .html extension. The browser is assuming that this is an HTML tag:

<?php echo "<p>

And is just printing everything after that. If you change your file to index.php you will get the outcome you're looking for.

Upvotes: 2

Related Questions