-1

For the same content below.

<html>
<body>
<?php  $name="jhon"; ?>
    <table>
        <tr>
            <td>name</td>
            <td><?php echo $name;?></td>
        </tr>
    </table>
</body>
</html>

1.To save it as test.php,the output in firefox is as below.

name    jhon

2.To save it as test.html,the output in firefox is as below.

name    

Why same content with different file type result in different output?

showkey
  • 482
  • 42
  • 140
  • 295
  • php-files are passed to the php-engine and therefore execute. all other files are regarded as static files and passed to the client without any operations. look at the page source and you will see your php-code - it's basically web server 101 – Franz Gleichmann Oct 17 '16 at 14:32

3 Answers3

1

If you save with a html extension, your server won't preprocess it, so the PHP block won't be evaluated. That means, the second tag containing the actual name won't be created.

An extension of .php tells the server to go through the file and run any containing PHP before sending the file to the client.

Carcigenicate
  • 43,494
  • 9
  • 68
  • 117
0

Because you have your webserver configured to pass *.php files through the PHP interpreter which executes the PHP contained within the PHP files.

HTML files aren't configured that way on your webserver (though you can configure it to do so). As it is, the server believes it is just a plain HTML document and is returning it verbatim, and the browser is interpreting your PHP open/close tags as HTML tags that it just doesn't recognize and doesn't know what to do with (try to view the source and see if your PHP code is showing up in the source).

Community
  • 1
  • 1
Jeff Lambert
  • 24,395
  • 4
  • 69
  • 96
0

Your webserver (Apache etc) can identify and evaluate only .php files as per default configuration AddType application/x-httpd-php .php

In order to execute any other scripts (say .html files) as PHP scripts you have to add AddType statement at your httpd.conf file like below (you have restart your webserver):

AddType application/x-httpd-php .html