简体   繁体   English

PHP正在HTML之外打印

[英]PHP is printing outside of HTML

I have a html page with php embedded inside which looks like this: 我有一个嵌入了php的html页面,如下所示:

<html>
<body>
<div>
<?php
function printTable(){
echo '<table></table>';
}
?>
</div>
</body>
</html>
<?php
printTable();
?>

when executed the html output is 执行时,html输出为

<html>
<body>
<div>
</div>
</body>
</html>
<table></table>

I want the table to be printed inside de DIV element. 我想将表格打印在de DIV元素内。 How can i do that? 我怎样才能做到这一点?

When you have a function that generates output, the output will be generated where that function is called , not where it is defined . 当您有一个生成输出的函数时,将在调用该函数的位置而不是定义的位置)生成输出。 You're calling it after the closing </html> tag, so it will be echoed at that point 您在结束</html>标记调用它,因此它会在此时回显

You are only calling your function after the tag. 您仅在标记后调用函数。 It should be called within your div block,ie 它应该在您的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>

Try this : 尝试这个 :

<html>
<body>
<div>
<?php
function printTable(){
echo '<table></table>';
}
printTable();
?>
</div>
</body>
</html>

Here, we call printTable() function inside the <div> . 在这里,我们在<div>内部调用printTable()函数。 So, Table will be printed in the div. 因此,表格将在div中打印。

<?php
function printTable(){
  return '<table></table>';
}
?>
<html>
 <body> 
 <div>
  <?php echo printTable(); ?>
 </div>
 </body>
</html>

You can use : 您可以使用 :

<?php
function printTable(){
  return '<table></table>';
}
?>
<html>
 <body> 
 <div>
  <?php echo printTable(); ?>
 </div>
 </body>
</html>

Or this: 或这个:

<html>
<body>
<div>
<?php
function printTable(){
echo '<table></table>';
}
printTable();
?>
</div>
</body>
</html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM