0

我的代码有问题。当我发布一些东西时,它会显示:

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>

预先感谢您帮助我!

4

2 回答 2

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;
}
于 2013-06-29T14:47:33.153 回答
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ų";
  }
}
于 2013-06-29T14:52:27.233 回答