Jump to content

[SOLVED] Simple code (switch/case) working 87% of the time


VarHyid

Recommended Posts

Hi, all :)

 

First off, it's a SOLVED problem which a) still keeps me thinking, but b) requires a question for the future. I'm actually still learning (by doing) PHP so maybe there's a simple reason for this case, in which case - sorry for bothering :)

 

I'm writing a very simple script that displays 1 of 4 possible events depending on a number in a (MySQL) database. It's really basic.

 

$ttest = $usrfield['ttest'];

switch ($ttest) {
case ($ttest < 50):
	$ttestr = "<div>Get at least 50 credits</div>";
	break;
case ($ttest > 49 && $ttest < 500):
	$ttestr = "<div><b>RANK 1</b></div><div style='color: #d00000; font-size: 10px;'>You have <b>".$ttest."</b> credits</div>";
	break;
case ($ttest >= 500 && $ttest < 5000):
	$ttestr = "<div><b>RANK 2</b></div><div style='color: #d00000; font-size: 10px;'>You have <b>".$ttest."</b> credits</div>";
	break;
case ($ttest >= 5000):
	$ttestr = "<div><b>RANK 3</b></div><div style='color: #d00000; font-size: 10px;'>You have <b>".$ttest."</b> credits</div>";
	break;
}

 

As I said - very basic. There are 15 (almost) EXACT codes, varying by the variable name (for example: $rtest + $rtestr, $ptest + $ptestr and so on) which make a document included in the "main" file which echoes all of them $ptestr, $rtestr, $ttestr etc.

 

The main document also sets

 

$usrfield = mysql_fetch_assoc($resultusr);

 

and $resultusr is a mysql_query selecting a particular user's row with ptest, rtest, ttest... named columns.

 

Here's the funny thing - it works perfectly in 13 out of 15 cases. In 2 cases, where the number is 0 it doesn't display "Get at least 50 credits", but returns "RANK 1..." instead (with 0 within the text). And it's not like these are the only two zeros - there are a 4 more and all 4 work fine and return "Get at least 50 credits".

 

I've been staring at the complete code half an hour, copying/pasting the working part that displays 0 properly and mass-replace "rtest" to "ttest" (for example) and still nothing. I've tried to check/repair the particular MySQL table - PhpMyAdmin says it's OK. All fields have same properties, all are 0-default, decimal(6,0). I've tried to make a separate case for $ttest equaling 0 and one for it being less than 50 and then 0 worked, but... nothing else, still returned 0-case even with 45671.

 

Then I've separated the first case and made an if (case1) else (switch):

 

$ttest = $usrfield['ttest'];

if ($ttest < 50) {
$ttestr = "<div>Get at least 50 credits</div>";
}
else {
switch ($ttest) {
	case ($ttest > 49 && $ttest < 500):
		$ttestr = "<div><b>RANK 1</b></div><div style='color: #d00000; font-size: 10px;'>You have <b>".$ttest."</b> credits</div>";
		break;
	case ($ttest >= 500 && $ttest < 5000):
		$ttestr = "<div><b>RANK 2</b></div><div style='color: #d00000; font-size: 10px;'>You have <b>".$ttest."</b> credits</div>";
		break;
	case ($ttest >= 5000):
		$ttestr = "<div><b>RANK 3</b></div><div style='color: #d00000; font-size: 10px;'>You have <b>".$ttest."</b> credits</div>";
		break;
}
}

 

and surprise, surprise - it works :o

 

So here's the question - is there something special about the 0 or about switch that it doesn't always work or only with particular numbers ( :wtf: )? Should I always mix if/else with switch in some particular situations? Or maybe ditch the switch completely and make it an if/elseif for... whatever reason?

I would replace you switch stament with an if/elseif/else block. Switch statements more or less are just used to test specific values that you know can come of that. So it would be something like this:

 

<?php
$dog = 'johnny';

switch($dog){
  case 'johnny':
    echo $dog;
    break;
  case 'another':
    echo 'not'.$johnny;
    break;
}

 

The only time I use switch is if I am checking against values that I know it will be. Hopefully that will help.

Normally a switch statement can not be used with multiple conditions, although it can be 'hijacked' to work like this, it's usually not a good idea. Here's why it's not working.. A switch statement compares the value in it's parameter to each case, if it's true then that case fires. Because you're usnig $ttest it compares (for example) $ttest to $ttest > 49 && $ttest < 50. The statement '$ttest > 49 && $ttest < 50' will evaluate to either true, or false. You want the case to fire if it's true. But when $ttest is 0 it's basically trying to compare false (because 0 == false) to the statement, giving you the exact opposite of what you want. Here's what happens:

 

$ttest = 0;
switch ($ttest) {
case ($ttest < 50): // $ttest < 50 evaluates to true, but you're comparing this true, to $ttest, and since $ttest == 0 == false you're getting: true != false so it goes to the next statement..
	$ttestr = "<div>Get at least 50 credits</div>";
	break;
case ($ttest > 49 && $ttest < 500): // Here you're getting false for '$ttest > 49 && $ttest < 500' and $ttest is == 0 == false, of course, false == false. which is why this one is triggering.
	$ttestr = "<div><b>RANK 1</b></div><div style='color: #d00000; font-size: 10px;'>You have <b>".$ttest."</b> credits</div>";
	break;
case ($ttest >= 500 && $ttest < 5000):
	$ttestr = "<div><b>RANK 2</b></div><div style='color: #d00000; font-size: 10px;'>You have <b>".$ttest."</b> credits</div>";
	break;
case ($ttest >= 5000):
	$ttestr = "<div><b>RANK 3</b></div><div style='color: #d00000; font-size: 10px;'>You have <b>".$ttest."</b> credits</div>";
	break;
}

 

Long story short, you can just use switch(true){...} instead of switch($ttest){...} to get the functionality that you're looking for. Although, I'd go for an if.. else if.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.