【问题标题】:Is there an isdate PHP function?有 isdate PHP 函数吗?
【发布时间】:2023-03-28 07:11:01
【问题描述】:

我在下面的用户函数中看到了一个名为 isdate 的内置函数,当我检查 PHP 文档时,我没有找到它。它是否存在,它有什么作用;或者这只是一个错字?

function mystery($a, $b, $c) {
$result = null;
if (strlen(trim($a)) == 0) {
    $result = $c;
}
else {
    if (strtolower(trim($b)) == "n") {
        if (!is_numeric($a)) {
            $result = $c;
        }
        else {
            $result = trim($a);
        }
    }
    else {
        if (strtolower(trim($b)) == "d") {
            if (!isdate($a)) {
                $result = $c;
            }
            else {
                $result = trim($a);
            }
        }
        else {
            $result = $a;
        }
    }
}
return($result);
}

【问题讨论】:

  • 你的标题错了——根本没有循环......
  • True J​​ohn31316,这里没有循环。已编辑。

标签: php date if-statement


【解决方案1】:

也许,这不是一个真正的答案,因为我不知道原始功能的用途,老实说我不在乎。不管怎样,你可以做一些简化以使其更具可读性,以避免重复处理,无用的$result 变量,特别是去除这些不可读的嵌套 if/else,换句话说,去除所有这些无用的噪音:

function mystery($a, $b, $c) {
    $trima = trim($a);

    if ( empty($trima) )
        return $c;

    $b = strtolower(trim($b));

    if ( $b == "n" )
        return is_numeric($trima) ? $trima : $c ;

    if ( $b == "d" )
        return isdate($trima) ? $trima : $c ;

    return $a;
}

我希望这能帮助你理解这个函数应该做什么(也许上下文会有所帮助)

【讨论】:

  • Casimir et Hippolyte 我明白你的沮丧。我认为这是一个不好的问题来测试某人。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-19
  • 2010-11-17
  • 2023-03-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多