4

[2017 年 8 月 12 日星期六 02:21:28.993810] [php7:notice] [pid 20352] [client :14302] PHP 注意:在 /var/www/zephyr/library/XenForo/Application.php 中遇到格式不正确的数值在线 1534

    /**
 * Gets the current memory limit.
 *
 * @return int
 */
public static function getMemoryLimit()
{
    if (self::$_memoryLimit === null)
    {
        $curLimit = @ini_get('memory_limit');
        if ($curLimit === false)
        {
            // reading failed, so we have to treat it as unlimited - unlikely to be able to change anyway
            $curLimit = -1;
        }
        else
        {
            switch (substr($curLimit, -1))
            {
                case 'g':
                case 'G':
                    $curLimit *= 1024; //This is line 1534
                    // fall through

                case 'm':
                case 'M':
                    $curLimit *= 1024;
                    // fall through

                case 'k':
                case 'K':
                    $curLimit *= 1024;
            }
        }

        self::$_memoryLimit = intval($curLimit);
    }

    return self::$_memoryLimit;
}

不太确定如何解决这个问题,有点难过,我指出第 1534 行

4

2 回答 2

2

您正在将字符串与 中的整数相乘$curLimit *= 1024;。因为$curLimit相等(例如)512M。所以你要做的是删除最后一个字符

$curLimitNumber = substr($curLimit, 0, -1);//Will extract the number (512 FROM 512M)
switch (substr($curLimit, -1))
        {
            case 'g':
            case 'G': 
                $curLimitNumber *= 1024;
于 2017-08-12T09:39:32.977 回答
0

$curLimit来自

$curLimit = @ini_get('memory_limit');

引用http://php.net/manual/en/ini.core.php#ini.memory-limit

使用整数时,该值以字节为单位。也可以使用本常见问题解答中描述的速记符号。

并引用文档中提到的常见问题解答:

可用的选项有 K(千字节)、M(兆字节)和 G(千兆字节;自 PHP 5.1.0 起可用),并且都不区分大小写。其他任何东西都假定字节。1M 等于 1 兆字节或 1048576 字节。1K 等于 1 千字节或 1024 字节。这些速记符号可以在 php.ini 和 ini_set() 函数中使用。

您显示的代码正在检查$curLimit取自的值中的最后一个字符memory_limit

switch (substr($curLimit, -1))

这意味着,它已经在期待速记符号了。在case块中,它检查 k、g、m 等(简写),然后扩展$curLimit为实际字节。

当你这样做

$value = "1M";
$value *= 1024;

结果将是 1024,但您会收到通知,因为“1M”不是格式正确的数值,而只是一个字符串。在这种情况下,PHP 所做的是将数字输入到第一个非数字字符,例如,它将“1M”视为整数 1。因此,代码可以工作,但它很草率。因此通知。

如果你想摆脱通知,你必须在乘以之前去掉速记$curLimit$curLimit转换为 int 或将其传递给。intval

XenForo 论坛中有一个关于此的错误报告:

于 2017-08-12T09:51:47.567 回答