创建一个 hello 脚本,命名为 hello.php:
1 <html> 2 <head> 3 <title>php test</title> 4 </head> 5 <body> 6 <?php echo '<p>hello world</p>'; ?> 7 </body> 8 </html>
上面的 html 代码中,在 php 代码中打印 hello。在 html 中编写 php 代码需要使用 <?php ?> tags。集成 php 和 html 是非常简单的事情。
我们也可以在 html 中编写更复杂的 php 代码,需要使用 <?php tag and end with ?> tag。
更高级的代码示例:
1 <html> 2 <head>php html integration</head> 3 <body> 4 <ul> <?php for($i=1;$i<=5;$i++){ ?> <li>item no <?php echo $i; ?></li> <?php } ?> </ul> 5 </body> 6 </html>
输出结果:
1 item no 1 2 item no 2 3 item no 3 4 item no 4 5 item no 5
推荐教程:php视频教程
以上就是html文件里可以写php代码吗的详细内容。
