我的代码有问题。当我发布某些内容时,它会显示:
SCREAM:
忽略错误抑制注意:未定义的变量:后缀在...
但是我已经在我的代码中定义了我的变量......那么我如何解决这个错误?
<?php
$conn = mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("test", $conn);
$time = time();
if(isset($_POST['pateikti'])) {
if(empty($_POST['tekstas'])) { echo "Tekstas per trumpas!"; }
else {
mysql_query("INSERT INTO tsp (timestamp, text) VALUES ('".time()."', '$_POST[tekstas]')");
}
}
$query = mysql_query("SELECT * FROM tsp");
while($row = mysql_fetch_assoc($query)) {
$diff = $time - $row['timestamp'];
switch(1) {
case ($diff < 60):
$ago = $diff;
if($ago == 0)
$ago == "akimirką";
else if($diff < 10)
$suffix = "sekundes";
else
$suffix = "sekundžių";
break;
}
echo $row['text']." buvo parasytas prieš ".$diff." ".$suffix."</br>";
}
?>
<form action="" method="POST" >
<input type="text" name="tekstas" /><br>
<input type="submit" name="pateikti" value="Pateikti" />
</form>
提前感谢您的帮助!
答案 0 :(得分:1)
因为你没有在开关之前定义它,所以在diff大于60的情况下使用默认设置
switch(1) {
case ($diff < 60):
$ago = $diff;
if($ago == 0)
$ago == "akimirką";
else if($diff < 10)
$suffix = "sekundes";
else
$suffix = "sekundžių";
break;
default:
$suffix ="default text";
break;
}
答案 1 :(得分:0)
您有以下开关:
switch(1) {
case ($diff < 60):
$ago = $diff;
if($ago == 0)
$ago == "akimirką";
else if($diff < 10)
$suffix = "sekundes";
else
$suffix = "sekundžių";
break;
}
此处,如果$diff
等于0
,则您$suffix
永远不会被定义。
只需将开关更改为:
$suffix = ''; // <-- define "suffix"
switch(1) {
case ($diff < 60):
$ago = $diff;
if($ago == 0)
$ago == "akimirką";
else if($diff < 10)
$suffix = "sekundes";
else
$suffix = "sekundžių";
break;
}
另外,恕我直言,该开关可以替换为:
$suffix = '';
if($diff < 60) {
$ago = $diff;
if($ago == 0) {
$ago == "akimirką";
}
else if($ago < 10) {
$suffix = "sekundes";
}
else {
$suffix = "sekundžių";
}
}