PHP 中的 if、else 语句
为体育网站编写 if-else 语句时遇到问题。关键是根据解析的分数将 $game
显示为 W
、L
或 Tie
来自名为 $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>B
和 A==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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试在最后两个“if”条件上使用“else if”而不是“if”。你还对结果进行explode()处理吗?
Try using "else if" instead of "if" on the last two "if" conditions. Also are you explode()'ing the result?
我还建议使用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.
或者在这里,如果你想要以一种神秘的方式
Or here, if you want it in a cryptic way