简体   繁体   中英

php IF is not working in my code

When i run this I allways get: (what ever is wdl form).

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

which means the last commands, the if on my php code is not working. the code:

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

how can i fix it?

the solusion was to remove the: ex: ; from if($_GET["wdl"] == "W");

Remove the ; at the end of the if , before the {

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

should be

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

and so on..

; is an instruction separator. Read more here

This is a common area of mistake, so to avoid this, you could do

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

That way you can avoid the accidental ; after the looping constructs

So the code block looks like this:

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

Because of semicolon( ; ) after IF statement

Remove semicolon after all IF statement. It looks like

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

Try this:

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

You need to remove the ; from the end of the if statements and you can also make it an else if to prevent it from testing the remaining statements once one matches.

is best way to replace if with switch statement

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM