在我的html文件中不能使用php代码

时间:2018-03-26 23:39:05

标签: php html

就在今天我要学习php,首先我安装了apache2服务器和mysql然后php-7。然后在“var / www / test.php”目录下我的php代码工作正常。但是,当我尝试在我的桌面中创建 index.html 文件时,我的PHP代码无法正常工作。这是我的index.html文件

   <!DOCTYPE html>
   <html>
   <head>
       <title>learnPhp</title>
   </head>
   <body>
        <h1>my first php code</h1>
       <?php
         echo "Hello World!";
       ?>
   </body>
   </html>

为了解决这个问题,我试着给出这两个帖子的所有方法:
1。 PHP codes doesn't work inside HTML codes
2. PHP inside HTML doesn't work

当我尝试重命名文件index.phn然后打开它时:
enter image description here

1 个答案:

答案 0 :(得分:-1)

您必须使用.php扩展名。

PHP文件仅适用于.php文件和服务器目录。

将文件重命名为index.php并将其复制到var / www / anything /

之后,转到http://localhost/anything/

编辑:我错了。您可以在html文件中运行php,但您必须在服务器的目录中有一个文件。例如/ www / ...并转到http://localhost/ ...