PHP:如何动态更改“if”条件顺序

发布于 2024-11-07 21:31:26 字数 820 浏览 4 评论 0原文

情况是这样的:有一个连续的循环,更新一些值。然后脚本检查某些条件。 (简化的)代码:

<?php
set_time_limit(0);
// etc
while(1==1)
{
    $a = getFromDatabase('a'); // function to get value of A
    $b = getFromDatabase('b'); // function to get value of B
    $c = getFromDatabase('c'); // function to get value of C
    $d = getFromDatabase('d'); // function to get value of D

    if($a >= 12 && time() <= $b && ($d === false || $d <= time()))
    {
         include 'pages/'.$a.'.php';
    }
    if($b <= 3 && time() >= $d && ($c === false || $c <= time()))
    {
         include 'pages/'.$b.'.php';
    }
}

我的问题是:如何动态更改这些 IF 语句的顺序?

重要提示

$order = array('b','a'); // first b then a

:if 条件确实是动态的。所以不存在真正的模式(上面的例子是简化的,所以不是完整的条件)

The situation is like this: there is a continuous loop, that updates some values. Then the script checks certain conditions. The (simplified) code:

<?php
set_time_limit(0);
// etc
while(1==1)
{
    $a = getFromDatabase('a'); // function to get value of A
    $b = getFromDatabase('b'); // function to get value of B
    $c = getFromDatabase('c'); // function to get value of C
    $d = getFromDatabase('d'); // function to get value of D

    if($a >= 12 && time() <= $b && ($d === false || $d <= time()))
    {
         include 'pages/'.$a.'.php';
    }
    if($b <= 3 && time() >= $d && ($c === false || $c <= time()))
    {
         include 'pages/'.$b.'.php';
    }
}

My question is: how can I change the order of these IF-statements dynamically?

Like

$order = array('b','a'); // first b then a

Important: the if conditions are really dynamic. So there is no real pattern (the example above is simplified, so not the full conditions)

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

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

发布评论

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

评论(2

夜巴黎 2024-11-14 21:31:26

将它们放入函数中,然后仅存储函数的名称并依次遍历每个函数。

function func1($a, $b, $c, $d)
{
  if (...)
  {
    return $a
  }
  return false;
}

function func2($a, $b, $c, $d)
 ...

$funcs = Array('func1', 'func2');

 ...

foreach($funcs as $func)
{
  if ($page = $func($a, $b, $c, $d))
  {
    include "pages/$page.php"
  }
}

Put them in functions, then just store the name of the functions and go through each in turn.

function func1($a, $b, $c, $d)
{
  if (...)
  {
    return $a
  }
  return false;
}

function func2($a, $b, $c, $d)
 ...

$funcs = Array('func1', 'func2');

 ...

foreach($funcs as $func)
{
  if ($page = $func($a, $b, $c, $d))
  {
    include "pages/$page.php"
  }
}
幻想少年梦 2024-11-14 21:31:26

不确定这是否是您想要的,但您可以将 if 放入函数中:

<?php
set_time_limit(0);
function a($a,$b,$c,$d){
        if($a >= 12 && time() <= $b && ($d === false || $d <= time()))
        {
             include 'pages/'.$a.'.php';
        }
}
function b($a,$b,$c,$d){
        if($b <= 3 && time() >= $d && ($c === false || $c <= time()))
        {
             include 'pages/'.$b.'.php';
        }
}

// etc
while(1==1)
{
    $a = getFromDatabase('a'); // function to get value of A
    $b = getFromDatabase('b'); // function to get value of B
    $c = getFromDatabase('c'); // function to get value of C
    $d = getFromDatabase('d'); // function to get value of D
    $order = array('b','a'); // first b then a
    foreach( $order as $fun)
        call_user_func_array($fun, array($a,$b,$c,$d))
}

Not sure if that's what you want, but you can put if in functions:

<?php
set_time_limit(0);
function a($a,$b,$c,$d){
        if($a >= 12 && time() <= $b && ($d === false || $d <= time()))
        {
             include 'pages/'.$a.'.php';
        }
}
function b($a,$b,$c,$d){
        if($b <= 3 && time() >= $d && ($c === false || $c <= time()))
        {
             include 'pages/'.$b.'.php';
        }
}

// etc
while(1==1)
{
    $a = getFromDatabase('a'); // function to get value of A
    $b = getFromDatabase('b'); // function to get value of B
    $c = getFromDatabase('c'); // function to get value of C
    $d = getFromDatabase('d'); // function to get value of D
    $order = array('b','a'); // first b then a
    foreach( $order as $fun)
        call_user_func_array($fun, array($a,$b,$c,$d))
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文