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

发布于 2024-07-17 18:08:15 字数 326 浏览 4 评论 0原文

有没有办法返回 switch 语句中的(case)实例总数? 类似这样的:

$id = $_GET['id'];

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

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

echo $data;

但其原因是存在多个包含这些 switch 语句的文件,但它们的数量因文件而异。 理想情况下,我希望能够通过增加“id”来循环这些 switch 语句。 关于这是否可能有什么想法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

兮子 2024-07-24 18:08:15

如果您只是根据另一个值分配值,您可以使用 数组 相反:

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

数组的优点是它可以是 扩展并且可以使用count()

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

别再吹冷风 2024-07-24 18:08:15

不是不改变 $id 的值并删除break语句..但这违背了目的。 您有理由需要知道有多少个吗?

我只会 grep 你想了解的文件

find -name '*php' | xargs grep 'case'

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'
墟烟 2024-07-24 18:08:15

啊 - 我想我明白你在追求什么。 您可以做的是添加一个 default: case 来终止循环,而不是尝试计数。 例如,

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

问题是:为什么不直接在没有循环和 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. 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?

三寸金莲 2024-07-24 18:08:15

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.

青萝楚歌 2024-07-24 18:08:15

实际上,您可以使用 token_get_all() 可靠地完成此操作。 以下是使用该函数查找所有define()用法的示例在 PHP 文件中。 您将需要构建一个有限状态机(类似于链接状态机)来查找 switch 语句,然后查找从属 case 语句。 您可能想也可能不想确保正确处理嵌套 switch 语句。

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.

美人迟暮 2024-07-24 18:08:15

好的,假设 URL 如下所示:

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

然后您可以获取 $_GET[id] 值并使用开关对其进行处理。

如果我正确地想象了你的代码:

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

}

如果那不正确,请原谅我的英语。 请再解释一下,还是有点模糊。

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.

零度° 2024-07-24 18:08:15

实际上,这段代码可以工作:

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

Actually, this code will work:

$i = 0;
switch(something)
{
    case "item".$i++: //something
        break;
    case "item".$i++: //something
        break;
    default: //something
        break;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文