我需要根据用户的状态显示不同的词汇。我意识到while()变量超出了Online_Status()的范围,但我无法弄清楚如何共享变量,甚至在Online_Status()中定义新变量,同时包含与while()相同的信息。我尝试在while(),while(),while()以及其他所有配置中使用Online_Status()几小时。没有发生。我将不胜感激任何帮助!
<?php
require_once('connect.php');
$con = mysqli_connect(DBHOST, DBUSER, DBPASS, DBNAME) or die('Connection failed: ' . mysqli_connect_error());
$sql = mysqli_query($con, "SELECT UserType, Online, InChat FROM membership WHERE UserType = 2 ORDER BY Online DESC");
while($row = mysqli_fetch_array($sql)){
$UserType = $row['UserType'];
$Online = $row['Online'];
$InChat = $row['InChat'];
echo Online_Status();
}
function Online_Status(){
if ($Online == 0) {
echo "I am not online. Please come back later";
}
else if($Online == 1 && $InChat == 0){
echo "I am Online and I will be in my chatroom shortly.";
}
else if($Online == 1 && $InChat == 1){
echo "I am Online chat with me now!";
}
}
mysqli_close($con);
?>
答案 0 :(得分:2)
使用global
关键字:
function Online_Status(){
global $Online;
if ($Online == 0) {
echo "I am not online. Please come back later";
}
}
最好通过函数参数传递函数:
function Online_Status($Online){
if ($Online == 0) {
echo "I am not online. Please come back later";
}
}
并称之为:
Online_Status($Online);
另外,请查看PHP
的{{3}}。