Robert Benedetto
Robert Benedetto

Reputation: 1720

Switch statement not working as expected

I have the follwing code:

switch ((int)$_GET["loanprovider"]) {
    case ((int)$_GET["loanprovider"] > 1):
        $sql_loanprovider1a = " l.id in (select la.loanid from loan_approvals la where la.bankid = ".$_GET["loanprovider"].") and";
        $sql_loanprovider1b = " l.loanid in (select la.loanid from loan_approvals la where la.bankid = ".$_GET["loanprovider"].") and";
        break;
    default:
        break;
}

If i run the page without setting LOANPROVIDER in the URL, the SWITCH still enters the first case and not the DEFAULT. How come??

/Bob

Upvotes: 0

Views: 866

Answers (4)

Amal
Amal

Reputation: 76646

The explanation is quite simple:

var_dump( (int)$_GET["loanprovider"]  );
var_dump( (int)$_GET["loanprovider"] > 1 );

The output is:

int(0)
bool(false)

They're both the same (the integer 0 (zero) is considered as FALSE), and case 1 will always get executed if the query parameter loanprovider is empty.

Refer to the PHP Manual for more information.


And, to actually fix the issue, use isset(). It's also a good idea to declare variables and store values if you need to re-use it later.

The following looks neater, in my opinion, and it works (tested):

if(isset($_GET['loanprovider'])) 
{
    $value = (int)$_GET["loanprovider"];
    switch ($value) {
        case ($value > 1):
            $sql_loanprovider1a = " l.id in (select la.loanid from loan_approvals la where la.bankid = ".$_GET["loanprovider"].") and";
            $sql_loanprovider1b = " l.loanid in (select la.loanid from loan_approvals la where la.bankid = ".$_GET["loanprovider"].") and";
            break;
        default:
            break;
    }
}

For debugging the issue, you can always use var_dump.

Upvotes: 2

kinske
kinske

Reputation: 607

I would first check if $_GET["loanprovider"] is set because if you don't set loanprovider in the url it will be undefined and undefined does not have to be "0" it can be something else. In this case it would be probably some value that makes your statement ((int)$_GET["loanprovider"] > 1): true.

The correct code would be:

if (isset((int)$_GET["loanprovider"]))
{
  switch ((int)$_GET["loanprovider"]) {
    case ((int)$_GET["loanprovider"] > 1):
        $sql_loanprovider1a = " l.id in (select la.loanid from loan_approvals la where la.bankid = ".$_GET["loanprovider"].") and";
        $sql_loanprovider1b = " l.loanid in (select la.loanid from loan_approvals la where la.bankid = ".$_GET["loanprovider"].") and";
        break;
    default:
        break;
  }
}  

Upvotes: 0

MD SHAHIDUL ISLAM
MD SHAHIDUL ISLAM

Reputation: 14523

Not tested but request to try it (though the coding is for >1)

But it will be wrong for negative value.

switch (intval($_GET["loanprovider"])) {
    case 0:
        break;
    case 1:
        break;
    default:
        $sql_loanprovider1a = " l.id in (select la.loanid from loan_approvals la where la.bankid = ".$_GET["loanprovider"].") and";
        $sql_loanprovider1b = " l.loanid in (select la.loanid from loan_approvals la where la.bankid = ".$_GET["loanprovider"].") and";
        break;

Upvotes: 0

BlueMonkMN
BlueMonkMN

Reputation: 25601

I'm no PHP expert, but I think your redundant use of loanprovider in the switch statement is a clue. I think if you don't set loanprovider you will have an empty value of some sort, which goes in the parameter to switch, and then you use a similar value in the case. assume (int)$_GET["loanprovider"] is 0. Then the expression (int)$_GET["loanprovider"] > 1 will also return 0 because 0 is not greater than 1. What you end up with is essentially:

switch (0) {
case 0:
  // your code
  break;
default:
  break;
}

Because 0 == 0, you end up in the first case.

Upvotes: 0

Related Questions