Page 1 of 1

0 is greater than 2 ? Surely not...

Posted: Wed Aug 11, 2004 2:56 am
by ihateevilbill
Awright dudes, have a wee problem with php. Maybe you lot can sort it out for me. I have this piece of code to check passenger ages against. However, when I ask the code if 0 is less than 2, it comes back false :S Heres the (ultra cut down version of the) offending code:

Code: Select all

<?php
$age[1]=0;
switch ($age[1]) {
case $age[1]<2: echo "age<2";break;
default: echo "age >2";break;
}
?>
So what do u think?

If I set $age[1]=1; then it works fine....wtf etc etc blah

Regards,

Steven.

Posted: Wed Aug 11, 2004 2:58 am
by feyd
it's inadvisable to use a switch in this case, as expressions are a bit odd in case statements. Unless you can post more of the real code, I'd suggest using an if..

Posted: Wed Aug 11, 2004 3:06 am
by ihateevilbill
A little update: In case you're wondering why Im using Switches instead of IF statements, well...the code is a helluva lot more than just this little piece here... there are switches and nested Ifs everwhere and it seemed easier to do the switch statement. PS I have a larger snippet of the code here just to show you that it does work with anything other than a 0 value:

Code: Select all

<?php
$age[1]=13;
switch ($age[1]) {
case $age[1]<2: echo "age < 2";break;
case $age[1]<19: echo "age > 2 and < 19";break;
default: echo "age > 2";
}
?>
This comes back with 'age > 2 and < 19' (ie 13<19). so no problems there then, heh

Cheers,

Steven.

Posted: Wed Aug 11, 2004 3:09 am
by feyd
it'd be a lot better to handle that as a couple ifs..

Posted: Wed Aug 11, 2004 3:09 am
by ihateevilbill
Cheers for replying so quickly Feyd,

Think ill be going to php.net with this one because the logic is fine. It's def the php interprator thats wrong in this case, lol.

Regards,

Steven.

Posted: Wed Aug 11, 2004 3:13 am
by ihateevilbill
Yeah true. Thing is, if this isnt fixed then IFs would sorta have to be used for this piece of code (same code, different section):

Code: Select all

function findpriceEur($daystravel) { // Area A
switch ($daystravel) {
 case $daystravel<=5:
  $travelcost="5.49";break;
 case $daystravel<=10:
  $travelcost="7.32";break;
 case $daystravel<=17:
  $travelcost="9.16";break;
 case $daystravel<=24:
  $travelcost="10.53";break;
 case $daystravel<=31:
  $travelcost="11.43";break;
 case $daystravel<=38:
  $travelcost="14.63";break;
 case $daystravel<=45:
  $travelcost="17.39";break;
 case $daystravel<=52:
  $travelcost="20.12";break;
 case $daystravel<=(31*2): //  2 months
  $travelcost="25.16";break;
 case $daystravel<=(31*3): //  3 months
  $travelcost="29.72";break;
 case $daystravel<=(31*4): //  4 months
  $travelcost="36.59";break;
 case $daystravel<=(31*5): //  5 months
  $travelcost="45.75";break;
 case $daystravel<=(31*6): //  6 months
  $travelcost="52.60";break;
 case $daystravel<=(31*7): //  7 months
  $travelcost="64.03";break;
 case $daystravel<=(31*8): //  8 months
  $travelcost="73.18";break;
 case $daystravel<=(31*9): //  9 months
  $travelcost="82.34";break;
 case $daystravel<=(31*10): //10 months
  $travelcost="91.46";break;
 case $daystravel<=(31*11): //11 months
  $travelcost="98.34";break;
 case $daystravel<=366:     //12 months
  $travelcost="105.19";break;
 }
return $travelcost;
}
:S

And theres 8 of these...and plenty more functions etc on the page.

dang ! lol

Posted: Wed Aug 11, 2004 3:15 am
by feyd
doing some checking it looks like, switch(0) always runs the default unless an exact match (case 0) is in the list.

Posted: Wed Aug 11, 2004 3:16 am
by ihateevilbill
See... thats the reason youre an admin :) cheers m8

Posted: Wed Aug 11, 2004 3:19 am
by ihateevilbill
Still think they should change the parser to accept switch(0)'s and change it to default all switch(NULLS). At least that would make sense :P

Posted: Wed Aug 11, 2004 3:24 am
by feyd
it'd make more sense to me to disallow expressions as cases..

that can be compacted into an array..loop combo..

Code: Select all

<?php

function findpriceEur($daystravel)
{
  $pricematch = array(
    array(5, 5.49),
    array(10, 7.32),
    array(17, 9.16),
    array(24, 10.53),
    array(31, 11.43),
    // .......
  );

  foreach($pricematch as $match)
  {
    if($daystravel <= $match[0])
      return $match[1];
  }

  return false;
}

Posted: Wed Aug 11, 2004 3:29 am
by ihateevilbill
Well, you definately learn something every day.

Will have to edit the code n make it look nicer now :) cheers again m8

SOLVED - Kinda

Posted: Wed Aug 11, 2004 1:52 pm
by scorphus
Please excuse me and let me be a bit ironic :twisted: :

Code: Select all

<pre><?php
$arg[1] = 0;
$arg[2] = 2;
$arg[3] = -1;
echo "Let's make the first test, with \$arg[1]:\n";
switch ($arg[1]) {
	case ($arg[1] == 0): // true
		echo 'case ($arg[1] == 0)';
		break;
	case ($arg[2] > 0): // true
		echo 'case ($arg[2] > 0)';
		break;
	case ($arg[3] < 0): // true
		echo 'case ($arg[3] < 0)';
		break;
	case (true):
		echo 'case (true)';
		break;
	default:
		echo 'Damn! Default??? How could!?';
		break;
}
echo "\nWell, let's make another test, now with \$arg[2]:\n";
switch ($arg[2]) {
	case ($arg[1] != 0): // false
		echo 'case ($arg[1] != 0)';
		break;
	case ($arg[2] < 0): // false
		echo 'case ($arg[2] < 0)';
		break;
	case ($arg[3] > 0): // false
		echo 'case ($arg[3] > 0)';
		break;
	case (false):
		echo 'case (false)';
		break;
	default:
		echo 'Damn! Default again! Why is it right now?';
		break;
}
echo "\nHmmm... let's get some tracks:\n";
if ($arg[1] == false)
	echo 'Ahh, $arg[1] is false! Or null...';
if ($arg[2] == true)
	echo ' And $arg[2] is true! Or not null...';
?></pre>
Hope you get it :P

Yours gently,
Scorphus.

Posted: Wed Aug 11, 2004 1:52 pm
by Weirdan
feyd wrote:doing some checking it looks like, switch(0) always runs the default unless an exact match (case 0) is in the list.
switch evaluates the given expression and then compares its value to each of the cases. When you have to use boolean expression in case it's usually written as:

Code: Select all

switch(true) {
  case $a>$b:
    // do something
    break;
  case $a == 3:
    // do something else
    break;
  default:
    // something
}