我在一个页面中有两个PHP代码块,但只有第一个运行
我有以下两段PHP代码,都在同一个页面上,但只有第一段运行。有什么问题吗 守则: 首先,确保正确打开和关闭HTML标记。然后将代码更改为以下内容:我在一个页面中有两个PHP代码块,但只有第一个运行,php,Php,我有以下两段PHP代码,都在同一个页面上,但只有第一段运行。有什么问题吗 守则: 首先,确保正确打开和关闭HTML标记。然后将代码更改为以下内容: <?php $username = null; $bash = null; if ($result -> num_rows > 0) { $row = $result->fetch_assoc(); $username = $row["username"];
<?php
$username = null;
$bash = null;
if ($result -> num_rows > 0) {
$row = $result->fetch_assoc();
$username = $row["username"];
$bash = $row["bash"];
}
?>
<div class = "row" >
<div class = "col-md-12">
enter code here
<h1 id = "username" style = "color:white; text-align:center">
<?= $username ?> <!-- OR --> <?php echo $username; ?>
</h1>
</div>
</div>
<div class = "row">
<div class = "col-md-12">
<h1 id = "bash" style = "color:blue; text-align:center">
<?= $bash ?> <!-- OR --> <?php echo $bash; ?>
</h1>
</div>
</div>
当您可以只执行一次PHP代码块时,绝对没有理由重复相同的PHP代码块两次。
您可以在条件语句之外启动两个变量,然后根据结果更改它们的值。
然后,您可以只回显h1标记中的变量,然后分别返回。
旁注:要在
<?php
$username = null;
$bash = null;
if ($result -> num_rows > 0) {
$row = $result->fetch_assoc();
$username = $row["username"];
$bash = $row["bash"];
}
?>
<div class = "row" >
<div class = "col-md-12">
enter code here
<h1 id = "username" style = "color:white; text-align:center">
<?= $username ?> <!-- OR --> <?php echo $username; ?>
</h1>
</div>
</div>
<div class = "row">
<div class = "col-md-12">
<h1 id = "bash" style = "color:blue; text-align:center">
<?= $bash ?> <!-- OR --> <?php echo $bash; ?>
</h1>
</div>
</div>