无法理解这个函数(getCache)在PHP中是如何工作的

Fer*_*rex 0 php

希望有人帮助我理解这一点:

function getCache($key, $timeout = 30) {
    $contents = false;
    if (MEMCACHE <> 0) {
         global $memcache;
         $contents = $memcache->get($key);
     }
    if (empty($contents)) {
        return false;
    }
    return $contents;
}

if($onlineFrnds = getCache($userid, 30)){
//do stuff
}
Run Code Online (Sandbox Code Playgroud)

我没有写这段代码,它是我试图修改的代码(因此要理解).

我无法解释的是setCache的调用是30以及为什么再次设置$ timeout = 30. Morevoer $ timeout未在函数中使用.如果30秒通过,则if条件的计算结果为true.

Lev*_*son 7

$timeout = 30是默认参数.这意味着如果您不提供它,参数将使用该值30.

这是一个小演示,向您展示它是如何工作的:

function demo($timeout = 30) {
    return $timeout;
}

echo demo(); // 30
echo demo(15); //15
Run Code Online (Sandbox Code Playgroud)

它看起来像是$timeout用于使缓存过期但从未使用过.