在php中切换条件案例

I use javascript to send cases to and ajax function. The cases are taken from the link id. For example, if user clicked this link: <a id="answer-yes-123">click this</a> The javascript will split the id to 3 part, and send the middle part "yes" to ajax as a case. For most cases, when ajax receives the case, if yes do this if no do that.

switch ($case) {
    case 'yes' :
        $assignment->add();
        break;
    case 'no' :
        $assignment->remove();
        break;

There's one exception-- a numerical case. If the middle part of the link is a number, I don't know how to make the switching statement. There are potentially unlimited different numbers, I can't make each of them a case, How to make a condition like if(is_int($case)) to work as a case?

In the switch default: case, you can test is_int():

switch ($case) {
    case 'yes' :
        $assignment->add();
        break;
    case 'no' :
        $assignment->remove();
        break;
    default:
        // Determine the numeric value however you need to 
        // is_int(), is_numeric(), preg_match(), whatever...
        if (is_int($case)) {
           // numeric stuff
        }
}

This is a little strange logically, because default: is typically used for the do this if nothing else is met condition. In your case though, if you don't need to further divide the numeric case much it works. Just be sure to comment it clearly in your code so you remember why you did it when you look back on it in six months.

Update after comments:

Since you already used the default:, I believe you can actually use an expression inside a case. This warrants even clearer commenting since it is not a common practice and goes kind of against the purpose of a switch:

switch ($case) {
    case 'yes' :
        $assignment->add();
        break;
    case 'no' :
        $assignment->remove();
        break;
    // This actually works, but is highly weird. 
    // One of those things I can't believe PHP allows.
    // is_int(), is_numeric(), preg_match(), whatever... 
    case is_int($case):
        // Numeric stuff
        break;
    default:
        // default stuff
}

Write the if-statement around the switch.

Something like this comes to my mind:

if (is_int($case)) {
   // ...
} else {
    switch ($case) {
        // ...
    }
}

Use PHP's is_numeric

http://php.net/manual/en/function.is-numeric.php

if (is_numeric($case)) {
   // ...
} else {
    switch ($case) {
        // ...
    }
}

Michael Berkowski's answer is perfect, but if don't want to use default case like this that could work also:

switch (preg_replace('/^[0-9]*$/','numeric',$case)) {
case 'yes' :
    $assignment->add();
    break;
case 'no' :
    $assignment->remove();
    break;
case 'numeric' :
    $assignment->remove();
    break;
default:
    //...
    break;
}