Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problem using if statment(one of my else if statement get ignored!)

Status
Not open for further replies.

Edward07

Programmer
Apr 13, 2007
49
NL
Hi all. i am making 2 diffrent calles to a php script and inside that php script i use if statment to do diffrent things. Unfortently the way i coded the if statment the second else if statment never get executed. It always get ignore and first else if get excuted instead!!

I want for each diffrent calls ONLY one of else if statments get excuted. could any one tell me what i am doing wrong here.Thanks



First type of call for first else if:
Code:
[URL unfurl="true"]http://localhost/script.php?cmd=file&file=visitorWantsToChat&site=20202020&channel=web&d=1177857195924&referrer=%28engage%29%20http%3A//localhost/1.html[/URL]
Second type of call for second else if:
Code:
[URL unfurl="true"]http://localhost/script.php?cmd=file&file=visitorWantsToChat&Pushed=true&site=20202020&channel=web&d=1177857103511&referrer=http%3A//localhost/1.html[/URL]

script.php
Code:
<?

$cmd   = $_GET['cmd'];
$file   = $_GET['file'];
$Pushed   = $_GET['Pushed'];

if ($cmd=="dive")
{
// do this1
}
else if ($cmd=="file" && $file=="visitorWantsToChat")
{
//do this2

}
else if ($cmd=="file" && $file=="visitorWantsToChat" && $Pushed=="true")
{


//do this3

}

?>
 
I don't know. I've long held that elseif is evil and should be avoided.

I'd write the code using a switch:

Code:
<?

$cmd   = $_GET['cmd'];
$file   = $_GET['file'];
$Pushed   = $_GET['Pushed'];

switch ($cmd)
{
   case "dive":
      //do this1
      break;

   case "file":
      if ($file=="visitorWantsToChat")
      {
         if ($Pushed == "true")
         {
            //do this3
         }
         else
         {
            //do this2
         }
      }
      break;
}
?>



Want the best answers? Ask the best questions! TANSTAAFL!
 
sleipnir214 thanks. you are right switch is beter method. It didn't came to my mind !!
 
The switch method would certainly be cleaner and easier to maintain.

You have to remember with "elseif" that the first matching condition causes its block to be executed, "else" the matching continues. In this case you could move the last test to the middle, or add '&& $Pushed != "true"' to the second test so it will no longer be true unless $Pushed is as well as the rest.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top