[英]PHP is printing outside of HTML
我有一個嵌入了php的html頁面,如下所示:
<html>
<body>
<div>
<?php
function printTable(){
echo '<table></table>';
}
?>
</div>
</body>
</html>
<?php
printTable();
?>
執行時,html輸出為
<html>
<body>
<div>
</div>
</body>
</html>
<table></table>
我想將表格打印在de DIV元素內。 我怎樣才能做到這一點?
當您有一個生成輸出的函數時,將在調用該函數的位置(而不是定義的位置)生成輸出。 您在結束</html>
標記后調用它,因此它會在此時回顯
您僅在標記后調用函數。 它應該在您的div塊中調用,即
<html>
<body>
<div>
<?php
function printTable(){
echo '<table></table>';
}
printTable();
?>
</div>
</body>
</html>
<html>
<body>
<div>
<?php
function printTable(){
echo '<table></table>';
}
printTable();
?>
</div>
</body>
</html>
嘗試這個 :
<html>
<body>
<div>
<?php
function printTable(){
echo '<table></table>';
}
printTable();
?>
</div>
</body>
</html>
在這里,我們在<div>
內部調用printTable()
函數。 因此,表格將在div中打印。
<?php
function printTable(){
return '<table></table>';
}
?>
<html>
<body>
<div>
<?php echo printTable(); ?>
</div>
</body>
</html>
您可以使用 :
<?php
function printTable(){
return '<table></table>';
}
?>
<html>
<body>
<div>
<?php echo printTable(); ?>
</div>
</body>
</html>
或這個:
<html>
<body>
<div>
<?php
function printTable(){
echo '<table></table>';
}
printTable();
?>
</div>
</body>
</html>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.