循环遍历 PHP 中的几个函数

发布于 2024-09-26 05:46:51 字数 420 浏览 10 评论 0原文

基本上我想实现连续执行 5-10 个函数的功能(就像通常一样)。但是,如果收到特定返回,我希望脚本返回几个步骤(例如从第 5 步回到第 3 步)并进一步继续(如 4、5、6、7、8、9、10)。示例:

<?
function_1st();
function_2nd();
function_3rd();
function_4th();
$a = function_5th();
if($a == '3') //continue from 3rd further;
function_6th();
function_7th();
?>

在本例中,它应该是 1,2,3,4,5,3,4,5,6,7,8,9,10。 在此使用面向对象编程(类)会更好吗?

基本上我只需要建议,如何以正确的方式做到这一点:)

问候, 乔纳斯

Basically I want to achieve such functionality that 5-10 functions are executed in a row (like normally). However, I want the script to go back several steps back (ex. from 5th back to 3rd) and continue further (like 4,5,6,7,8,9,10), if specific return is received. Example:

<?
function_1st();
function_2nd();
function_3rd();
function_4th();
$a = function_5th();
if($a == '3') //continue from 3rd further;
function_6th();
function_7th();
?>

Like in this case it should be 1,2,3,4,5,3,4,5,6,7,8,9,10.
Would it be better to use object orientated programming (class) in this?

Basically I need only advice, how to make it like this in a proper waY :)

Regards,
Jonas

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

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

发布评论

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

评论(5

万人眼中万个我 2024-10-03 05:46:51

将所有函数包装在一个开关中,如下所示:

function run($n) {
    switch($n) {
      case 1: $a = func1(); break;
      case 2: $a = func2(); break;
      case 3: $a = func3(); break;
      case 4: $a = func4(); break;
      case 5: $a = func5(); break;
      case 6: $a = func6(); break;
      case 7: $a = func7(); break;
      case 8: $a = func8(); break;
      case 9: $a = func9(); break;
      default: return $a;
    }
    run($a ? $a : $n+1);
}

wrap all the functions in a switch something like:

function run($n) {
    switch($n) {
      case 1: $a = func1(); break;
      case 2: $a = func2(); break;
      case 3: $a = func3(); break;
      case 4: $a = func4(); break;
      case 5: $a = func5(); break;
      case 6: $a = func6(); break;
      case 7: $a = func7(); break;
      case 8: $a = func8(); break;
      case 9: $a = func9(); break;
      default: return $a;
    }
    run($a ? $a : $n+1);
}
何以笙箫默 2024-10-03 05:46:51

您可以将函数调用基于上一个函数返回

示例:

function one() {
   if()...
      return 'run function two';
   else 
      return 'run function four';
}

You could base the function calls on the previous function return

Example:

function one() {
   if()...
      return 'run function two';
   else 
      return 'run function four';
}
亚希 2024-10-03 05:46:51
$step=1;
while($step < 99){
switch ($step){
    case 1:
        $step=function_1();
        break;
    case 2:
        $step=function_2();
        break;
    case 3:
        $step=function_3();
        break;
    ...
    case 9:
        $step=function_9();
        break;
    default:
        $step=99;
        }
    }
$step=1;
while($step < 99){
switch ($step){
    case 1:
        $step=function_1();
        break;
    case 2:
        $step=function_2();
        break;
    case 3:
        $step=function_3();
        break;
    ...
    case 9:
        $step=function_9();
        break;
    default:
        $step=99;
        }
    }
夜巴黎 2024-10-03 05:46:51

使用“变量函数”或“call_user_func()函数”。我没有时间解释,请谷歌搜索关键字。

Use "variable functions" or "call_user_func() function". I don't have time to explain it, please google that keywords.

油焖大侠 2024-10-03 05:46:51

为什么需要这样做?您是说,如果在调用 function_5() 之后调用 function_3() ,那么 $a 上的结果会有所不同,然后它将通过 function_5() 的检查?

您可能需要重新思考您的总体逻辑。

顺便说一句,不要使用短标签。写出

<?php

Why would you need to do this? Are you saying that the results on $a will be different if function_3() is called on it after function_5() has been called on it and then it will pass the check on function_5() ?

You may need to rethink your logic in general.

By the way, don't use short tags. Write out

<?php
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文