PHP 中的 if、else 语句

发布于 2024-11-09 11:37:48 字数 658 浏览 1 评论 0原文

为体育网站编写 if-else 语句时遇到问题。关键是根据解析的分数将 $game 显示为 WLTie来自名为 $row["result"] 的变量的 MySQL 表。表中列的类型为VARCHAR,保存数据的格式为$row["result"] = "A:B",其中A为主队得分,B为对手得分。我遇到了一个问题,我编写 if 语句时只能正确回显 Wins (W) 和 Ties (Tie)!

例如,

<?php
$wl = $row["result"];
if ($wl[1] > $wl[3]) {
  $game = "W";
}
if ($wl[1] < $wl[3]) {
  $game = "L";
}
if ($wl[1] == $wl[3]) {
  $game = "Tie";
}
?>

$wl= A>BA==B 时,$game 将正确输出,但 A时则不会正确输出。 B。我有一种感觉,这与 PHP 将 $wl 中的数据解释为不是数字,而是其他格式有关......

Problem with writing an if-else statement for a sports website. The key is to display $game as W, L, or Tie depending on the scores which are parsed from a MySQL table from a variable called $row["result"]. The type of the column in table is VARCHAR and format the data saved in is $row["result"] = "A:B" where A is the home team's score and B is the opponents score. I am running into a problem where I write the if statement I can only echo Wins (W) and Ties (Tie) correctly!

For example the code:

<?php
$wl = $row["result"];
if ($wl[1] > $wl[3]) {
  $game = "W";
}
if ($wl[1] < $wl[3]) {
  $game = "L";
}
if ($wl[1] == $wl[3]) {
  $game = "Tie";
}
?>

$game will output correctly when $wl= A>B and A==B but not A<B. I have a feeling this has something to do with PHP interpreting the data from $wl as not numbers, but some other format...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

只有影子陪我不离不弃 2024-11-16 11:37:48

尝试在最后两个“if”条件上使用“else if”而不是“if”。你还对结果进行explode()处理吗?

$wl = explode(':', $row['result']);
$wl[0] = Score for team A
$wl[1] = Score for team B

Try using "else if" instead of "if" on the last two "if" conditions. Also are you explode()'ing the result?

$wl = explode(':', $row['result']);
$wl[0] = Score for team A
$wl[1] = Score for team B
嘦怹 2024-11-16 11:37:48

我还建议使用explode(),但对于您使用的内容,我认为您应该引用 $wl[0] 和 $wl[2],因为数组 $wl 从零索引开始。

I'd also recommend using explode(), but for what you're using I think you should be referencing $wl[0] and $wl[2], since the array $wl starts at zero index.

终陌 2024-11-16 11:37:48

或者在这里,如果你想要以一种神秘的方式

 $wl = explode(":",$row["result"]);
 $game = ($wl[0]>$wl[1])?"W":(($wl[0]<$wl[1])?"L":"TIE");

Or here, if you want it in a cryptic way

 $wl = explode(":",$row["result"]);
 $game = ($wl[0]>$wl[1])?"W":(($wl[0]<$wl[1])?"L":"TIE");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文