PHP输出echo错误的顺序

时间:2016-04-18 14:04:17

标签: php html

我在PHP中输出有问题我正在尝试打印出来: 欢迎回来! 管理员 你是测试者编号:3 然后在按钮Logout和Comment下面。 但它首先输出按钮然后输出文本。

Output result

代码:



<?php
session_start();
if (isset($_SESSION['id'])) {
	// session variables into local variables.
	$id = $_SESSION['id'];
	$username = $_SESSION['username'];
	$result = "Welcome back! <br>".$username. "<br> You are tester number: ".$id;
	echo ' <button class="btn" type="button" onclick=window.parent.location.href="logout.php" target="_parent">Log out</button>
	<button class="btn" type="button" onclick=window.parent.location.href="blog/post.php" target="_parent">Comment</button>
	';

	} else {
		$result = "You are not logged in yet";

	}

?>
<?php
echo $result;
?>
<title>Welcome - <?php echo $username ;?></title>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

在回显欢迎文本之前,您将回显按钮的html部分。

你能做什么:

$result = "Welcome back! <br>".$username. "<br> You are tester number: ".$id;
$result .= ' <button class="btn" type="button" onclick=window.parent.location.href="logout.php" target="_parent">Log out</button>
<button class="btn" type="button" onclick=window.parent.location.href="blog/post.php" target="_parent">Comment</button>
';

答案 1 :(得分:0)

你正在回显按钮然后$ result ...执行此操作:

<?php
 session_start();
 if (isset($_SESSION['id'])) {
  // session variables into local variables.
   $id = $_SESSION['id'];
   $username = $_SESSION['username'];
   $result = "Welcome back! <br>".$username. "<br> You are tester number: ".$id."</br>";
   $result.=' <button class="btn" type="button" onclick=window.parent.location.href="logout.php" target="_parent">Log out</button><button class="btn" type="button" onclick=window.parent.location.href="blog/post.php" target="_parent">Comment</button>';

?>
<?php
   echo $result;
?>
<title>Welcome - <?php echo $username ;?></title>

希望它有所帮助。