0

当我运行它时,我总是得到:(什么是 wdl 形式)。

copy ("templates/colors/win.txt", "tips/$today/color.txt");
copy ("templates/text/win.txt", "tips/$today/wdl.txt");

这意味着最后一个命令,我的 php 代码上的if不起作用。编码:

<?php
echo ("Setting up colors...");
if($_GET["wdl"] == "D");
{
    copy ("templates/colors/dr.txt", "tips/$today/color.txt");
    copy ("templates/text/dr.txt", "tips/$today/wdl.txt");
}
if($_GET["wdl"] == "L");
{
    copy ("templates/colors/lose.txt", "tips/$today/color.txt");
    copy ("templates/text/lose.txt", "tips/$today/wdl.txt");
}
if($_GET["wdl"] == "W");
{
    copy ("templates/colors/win.txt", "tips/$today/color.txt");
    copy ("templates/text/win.txt", "tips/$today/wdl.txt");
}
?>

我该如何解决?

解决方案是删除:例如:;if($_GET["wdl"] == "W");

4

4 回答 4

9

去掉;末尾的if, 之前的{

if($_GET["wdl"] == "D");
{

应该

if($_GET["wdl"] == "D")
{

等等..

;是指令分隔符。在这里阅读更多

这是一个常见的错误区域,因此为避免这种情况,您可以这样做

if($_GET["wdl"] == "D") {
   ...

这样你就可以避免;循环构造之后的意外

所以代码块看起来像这样:

<?php
    echo ("Setting up colors...");
    if($_GET["wdl"] == "D") {
        copy ("templates/colors/dr.txt", "tips/$today/color.txt");
        copy ("templates/text/dr.txt", "tips/$today/wdl.txt");
    }
    if($_GET["wdl"] == "L") {
        copy ("templates/colors/lose.txt", "tips/$today/color.txt");
        copy ("templates/text/lose.txt", "tips/$today/wdl.txt");
    }
    if($_GET["wdl"] == "W") {
        copy ("templates/colors/win.txt", "tips/$today/color.txt");
        copy ("templates/text/win.txt", "tips/$today/wdl.txt");
    }
?>
于 2013-09-03T13:55:11.393 回答
4
if($_GET["wdl"] == "W");

因为语句后面有分号( ;)IF

在所有语句后删除分号IF。看起来像

if($_GET["wdl"] == "D") {
    copy ("templates/colors/dr.txt", "tips/$today/color.txt");
    copy ("templates/text/dr.txt", "tips/$today/wdl.txt");
}
if($_GET["wdl"] == "L") {
    copy ("templates/colors/lose.txt", "tips/$today/color.txt");
    copy ("templates/text/lose.txt", "tips/$today/wdl.txt");
}
if($_GET["wdl"] == "W") {
    copy ("templates/colors/win.txt", "tips/$today/color.txt");
    copy ("templates/text/win.txt", "tips/$today/wdl.txt");
}
于 2013-09-03T13:55:53.803 回答
2

尝试这个:

if($_GET["wdl"] == "D"){
    copy ("templates/colors/dr.txt", "tips/$today/color.txt");
    copy ("templates/text/dr.txt", "tips/$today/wdl.txt");
}else if($_GET["wdl"] == "L"){
    copy ("templates/colors/lose.txt", "tips/$today/color.txt");
    copy ("templates/text/lose.txt", "tips/$today/wdl.txt");
}else if($_GET["wdl"] == "W"){
    copy ("templates/colors/win.txt", "tips/$today/color.txt");
    copy ("templates/text/win.txt", "tips/$today/wdl.txt");
}

您需要;从语句的末尾删除if,您还可以将其设为 anelse if以防止它在匹配时测试剩余的语句。

于 2013-09-03T13:57:24.147 回答
1

是用 switch 语句替换 if 的最佳方法

<?php
echo ("Setting up colors...");
switch($_GET["wdl"]) {
  case "D" :
       copy ("templates/colors/dr.txt", "tips/$today/color.txt");
       copy ("templates/text/dr.txt", "tips/$today/wdl.txt");
       break;
  case "W" :
     copy ("templates/colors/win.txt", "tips/$today/color.txt");
     copy ("templates/text/win.txt", "tips/$today/wdl.txt");
      break;
  case "L" :
     copy ("templates/colors/lose.txt", "tips/$today/color.txt");
     copy ("templates/text/lose.txt", "tips/$today/wdl.txt");
      break;
 }
?>
于 2013-09-03T14:01:58.700 回答