views:

300

answers:

7

Is there a way to return the total number of (case) instances there are in a switch statement? Something like this:

$id = $_GET['id'];

switch ($id) {
  case "item1" :
  $data = 'something1';
  break;

  case "item2" :
  $data = 'something2';
  break;
}

echo $data;

But the reasoning for it is there are multiple files with these switch statements in them, but the number of them vary depending on the file. I'd ideally like to be able to loop through these switch statements by incrementing the "id". Any ideas on if this is possible?

+1  A: 

Not without altering the value of $id and removing break statements.. but that kind of defeats the purpose. Is there a reason you need to know how many?

I would just grep the files you want to find out about

find -name '*php' | xargs grep 'case'
jim
+6  A: 

If you’re just assigning values based on another value you could use an array instead:

$idToData = array(
    'item1' => 'something1',
    'item2' => 'something2',
);
$data = 'default';
if (isset($_GET['id']) && array_key_exists($_GET['id'], $idToData)) {
    $data = $idToData[$_GET['id']];
}
echo $data;

The advantage of an array is that it can be extended and the number of items can be counted with count.

Gumbo
Was just in the middle of writing that, +1. If all there is here is an $id/$data correlation, an array sounds like the right tool for the job.
zombat
+1  A: 

Ah - I think I see what you're after. What you could do is add a default: case that terminates the loop, rather than trying to count. E.g.

for($id = 1; !$quit; $id++)
{
    switch("item" . $id) {
    case "item1":
         // Do something
         break;
    case "item<n>":
         // Do something else
         break;
    default:
         $quit = true;
    }
}

Question is: why not just do all this without a loop and case statements by just ... putting one statement after another?

Jason Musgrove
I agree I think this was the answer he was after, but I don't quite get how its going to help with all the other files XDI guess he could loop the function call on each file while incrementing the id. Then he could just check for the defaulting return condition or null if there is none.
jim
+1  A: 

You can probably do what you're asking with token_get_all(), but chances are that's not really the best solution to your actual problem.

Frank Farmer
A: 

OK, let's say the URL looks like this:

somesite.com/ajax/getinfo.php?id=news

Then you can take $_GET[id] value and process it with a switch.

If I imagined your code correcly:

$section=$_GET[id];
switch($section) {
case "1":
break;
.
.
.
default:

}

If that is not correct, so pardon my English. Please explain it a bit more, it stills a bit ambiguous.

Moutaz
+1  A: 

Actually you can do this reliably using token_get_all(). Here is an example of using that function to find all the define() usages in a PHP file. You will need to build a finite state machine (similar to the linked one) to look for switch statements and then the subordinate case statements. You may or may not want to make sure you deal with nested switch statements correctly.

cletus
A: 

Actually, this code will work:

$i = 0;
switch(something)
{
    case "item".$i++: //something
        break;
    case "item".$i++: //something
        break;
    default: //something
        break;
}
WebDevHobo