php中嵌入html有多种方式,这里列出几种常用方式:
1、用echo输出HTML代码
<?php $int=rand(0,1); if($int==1){ echo "<p>取到的随机数是1</p>"; }else{ echo "<p>取到的随机数不是1</p>"; } ?>
2、HTML代码中嵌入PHP
立即学习“PHP免费学习笔记(深入)”;
这样可以在大段大段的html代码的各个需要执行php的地方添加代码了。
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta http-equiv="Content-Language" content="zh-CN" /> <title>Hello World</title> </head> <body> <?php echo "Hello world!这是正文"; ?> </body> </html>
3、使用(
这是在PHP168的模板代码中首次见到的
<?php print <<<EOT <div class="slidecont">{$label[deepblue_mainslide]}</div> <div class="newcontainter"> <div class="head">{$label[deepblue_mainh1]}</div> <div class="cont" id="Tab1">{$label[deepblue_maint1]}</div> <div class="cont" id="Tab2">{$label[deepblue_maint2]}</div> </div> <a href="$rs[url]" title="$rs[descrip]" target="_blank">$rs[name]</a> EOT; ?>
4、直接引用HTML文件进PHP中
一个HTML文件(test.html),主要代码为:
<body> <p>我是一段Html代码</p> </body>
使用php的include()函数将外部的HTML文件连接到PHP文件中
<?php include ("test.html"); ?>
输出:
以上就是php中怎么嵌入html代码的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号