PHP Switch case behavior: ranges

Would it be so bad to have ranges for switch cases?

...
switch($var)
	{
	case < = 9:
		{
		echo "less than ten";
		break;
		}
	case 10:
		{
		echo "ten exactly";
		break;
		}
	case >10 <> <15:
		{
		echo "a range of values? why not?";
		break;
		}
	case >= 15:
		{
		echo "greater than or equal to fifteen";
		break;
		}
	default:
		{
		echo "what am I not thinking of here?"
		break;
		}
	}
...
Share this:
Share this page via Email Share this page via Stumble Upon Share this page via Digg this Share this page via Facebook Share this page via Twitter
Bookmark and Share

  • Marc Cabrera

    Actually this won’t work, just tested. Try this instead:

    switch ( True ) {
    case ($tmpVar == 729 && $intVal<= 20):
    echo 21;
    break;
    case ($tmpVar == 829 && $intVal<= 20):
    echo 22;
    break;
    case ($tmpVar == 729 && $intVal <= 40):
    echo 40;
    break;
    }

  • Marc Cabrera

    Actually this won’t work, just tested. Try this instead:

    switch ( True ) {
    case ($tmpVar == 729 && $intVal<= 20):
    echo 21;
    break;
    case ($tmpVar == 829 && $intVal<= 20):
    echo 22;
    break;
    case ($tmpVar == 729 && $intVal <= 40):
    echo 40;
    break;
    }

  • suzza

    switch($var)
    {
    case $var10 <15: though…

  • suzza

    switch($var)
    {
    case $var10 <15: though…

  • suzza

    switch($var)
    {
    case $var<=9:
    {
    echo “less than ten”;
    break;
    }

  • suzza

    switch($var)
    {
    case $var<=9:
    {
    echo “less than ten”;
    break;
    }

  • steve cooley

    nicely done. :)

  • steve cooley

    nicely done. :)

  • Elmware

    Even Turbo Pascal has ranges for case. You just simply put 1..10 as 1 to 10.

    Would be nice if they had this in php. Guess I’m just gonna have to use a bunch of if and elseif statements.

  • Ancient Items

    because ur constant is not valid.. u  can pt ur statement after the costant.

    for ex.
    switch (variable)
    statement
    break;