如何在 <?php ?> 中编写 html 代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18140270/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to write html code inside <?php ?>
提问by Karan Bhatia
I want to create table inside php script .. Is there any way that i could create table inside php script.?
我想在 php 脚本中创建表 .. 有什么办法可以在 php 脚本中创建表吗?
<?php html code to create table ?>
回答by Gautam3164
You can do like
你可以像
HTML in PHP :
PHP 中的 HTML :
<?php
echo "<table>";
echo "<tr>";
echo "<td>Name</td>";
echo "<td>".$name."</td>";
echo "</tr>";
echo "</table>";
?>
Or You can write like.
或者你可以这样写。
PHP in HTML :
HTML 中的 PHP :
<?php /*Do some PHP calculation or something*/ ?>
<table>
<tr>
<td>Name</td>
<td><?php echo $name;?></td>
</tr>
</table>
<?php /*Do some PHP calculation or something*/ ?>
Means:
You can open a PHP tag with <?php
, now add your PHP code, then close the tag with ?>
and then write your html code. When needed to add more PHP, just open another PHP tag with <?php
.
<?php /*Do some PHP calculation or something*/ ?>
意思是:
你可以用 来打开一个 PHP 标签<?php
,现在添加你的 PHP 代码,然后用 关闭标签,?>
然后编写你的 html 代码。当需要添加更多 PHP 时,只需打开另一个带有<?php
.
回答by Phil
You can drop in and out of the PHP contextusing the <?php
and ?>
tags. For example...
您可以使用和标签进入和退出 PHP上下文。例如...<?php
?>
<?php
$array = array(1, 2, 3, 4);
?>
<table>
<thead><tr><th>Number</th></tr></thead>
<tbody>
<?php foreach ($array as $num) : ?>
<tr><td><?= htmlspecialchars($num) ?></td></tr>
<?php endforeach ?>
</tbody>
</table>
Also see Alternative syntax for control structures
另请参阅控制结构的替代语法
回答by Rohan Kumar
Try it like,
试试看,
<?php
$name='your name';
echo '<table>
<tr><th>Name</th></tr>
<tr><td>'.$name.'</td></tr>
</table>';
?>
Updated
更新
<?php
echo '<table>
<tr><th>Rst</th><th>Marks</th></tr>
<tr><td>'.$rst4.'</td><td>'.$marks4.'</td></tr>
</table>';
?>
回答by Notepad
Can place code anywhere
可以在任何地方放置代码
<input class="my_<? print 'test' ?>" />