php IF在我的代码中不起作用

时间:2013-09-03 13:54:34

标签: php if-statement

当我运行时,我总是得到:(什么是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");
}
?>

我该如何解决?

解决方法是从;

中移除:ex:if($_GET["wdl"] == "W");

4 个答案:

答案 0 :(得分:10)

;

之前移除if末尾的{
if($_GET["wdl"] == "D");
{

应该是

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

依旧......

;是一个指令分隔符。 Read more here

这是一个常见的错误区域,所以为了避免这种情况,你可以做到

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");
    }
?>

答案 1 :(得分: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");
}

答案 2 :(得分: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,并且还可以将其设为else if,以防止它在匹配后测试剩余的语句。

答案 3 :(得分: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;
 }
?>