3

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?

Gumbo
  • 643,351
  • 109
  • 780
  • 844
  • 2
    I really don't understand what you're trying to do. Maybe if you explained the problem you're trying to solve, we could help with that. A solution of "counting case statements" seems like it can't be the best approach. – Chad Birch Apr 27 '09 at 21:55
  • Neither do I, but it seems to me there is a better solution for what ever you want to do. – Moutaz Apr 27 '09 at 22:36
  • I'm just trying to not rewrite someone else's code that generally does what we want it to :) I'm basically creating an index of all of the sections of the site for a non-database search engine. The person who built the site used jquery to load portions of data from some switch statements. Jquery would load the url of the php file with an ID variable passed in it's url, which the switch/case would return the proper corresponding data. Since there's no database, I thought to loop through the switches and comparing a string, it could then return the right one. That's the nutshell... better ideas? –  Apr 28 '09 at 03:00

7 Answers7

7

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()

NullUserException
  • 83,810
  • 28
  • 209
  • 234
Gumbo
  • 643,351
  • 109
  • 780
  • 844
  • 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 Apr 27 '09 at 21:58
1

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'
lsl
  • 4,371
  • 3
  • 39
  • 54
1

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
  • 3,574
  • 19
  • 14
  • 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 XD I 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. – lsl Apr 27 '09 at 22:22
1

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
  • 38,246
  • 12
  • 71
  • 89
1

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.

Community
  • 1
  • 1
cletus
  • 616,129
  • 168
  • 910
  • 942
0

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
  • 156
  • 1
  • 3
  • 8
0

Actually, this code will work:

$i = 0;
switch(something)
{
    case "item".$i++: //something
        break;
    case "item".$i++: //something
        break;
    default: //something
        break;
}
KdgDev
  • 14,299
  • 46
  • 120
  • 156