简体   繁体   English

在PHP中,我可以在switch语句中获得case语句的总数吗?

[英]In PHP can I get the total number of case statements in a switch statement?

Is there a way to return the total number of (case) instances there are in a switch statement? 有没有办法在switch语句中返回(case)实例的总数? 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. 但其原因是它们中有多个带有这些switch语句的文件,但它们的数量因文件而异。 I'd ideally like to be able to loop through these switch statements by incrementing the "id". 理想情况下,我希望能够通过递增“id”来遍历这些switch语句。 Any ideas on if this is possible? 关于这是否可行的任何想法?

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() 数组的优点是它可以扩展,并且可以使用count()计算项目count()

Not without altering the value of $id and removing break statements.. but that kind of defeats the purpose. 并非没有改变$ id的值并删除break语句..但这种方法失败了。 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'

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. 您可以做的是添加一个默认值:case终止循环,而不是尝试计数。 Eg 例如

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? 问题是:为什么不只是在没有循环和case语句的情况下完成所有这些......只需要一个接一个的语句?

您可以使用token_get_all()执行您所要求的操作 ,但很可能这不是您实际问题的最佳解决方案。

Actually you can do this reliably using token_get_all() . 实际上,您可以使用token_get_all()可靠地执行此操作。 Here is an example of using that function to find all the define() usages in a PHP file . 下面是使用该函数查找PHP文件中所有define()用法的示例。 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. 您将需要构建一个有限状态机(类似于链接的状态机)来查找switch语句,然后查找从属case语句。 You may or may not want to make sure you deal with nested switch statements correctly. 您可能希望也可能不希望确保正确处理嵌套的switch语句。

OK, let's say the URL looks like this: 好吧,假设URL看起来像这样:

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

Then you can take $_GET[id] value and process it with a switch. 然后你可以获取$ _GET [id]值并用开关处理它。

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. 请再解释一下,它仍然有点含糊不清。

Actually, this code will work: 实际上,此代码将起作用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM