setcookie函數是不是只能用字串進行賦值?
下面的程式碼中的mycookie方法中的setcookie是不是沒有辦法用陣列來賦值? ?
<?php
date_default_timezone_set('Asia/Shanghai');
//变量赋值
$cookie_name = (string)time();
$cookie_value = date('Y-m-d_H:i:s');
$cookie_expire = (time() + 60);
$cookie_path = '/untitled/';
$cookie_domain = '';
$cookie_secure = false;
$cookie_httponly = true;
//定义各个数组
$MyConfig['Cookie'] = array(
'name' => $cookie_name,
'value' => $cookie_value,
'expire' => $cookie_expire,
'path' => $cookie_path,
'domain' => $cookie_domain,
'secure' => $cookie_secure,
'httponly' => $cookie_httponly,
);
//查看数组情况
foreach ($MyConfig['Cookie'] as $item => $value) {
var_dump($item . var_dump($value));
echo '<br>';
}
//最终给COOKIE赋值
function mycookie()
{
setcookie( $MyConfig['Cookie']['name'], $MyConfig['Cookie']['value'],);
//**这一行如何才能使用数组进行赋值**
}
?>
感謝告訴一個方法。
如果php版本在5.6及之後,且在保證$Myconfig['Cookie']的下標順序與setcookie的參數順序一致的情況下,可以寫成
setcookie(...$Myconfig['Cookie'])
請參閱php手冊:使用... 運算子進行參數展開