php compact内置函数
据我目前所知,这个内置函数主要作用是 可以让你的代码更清晰,节省代码量
我们先看一下正常的写法:
$name = "鱼"; $age = "10086"; $sex = "保密"; $phone = "10010"; $result['name'] = $name; $result['age'] = $age; $result['sex'] = $sex; $result['phone'] = $phone; dump($result);
这个是 用compact的写法:
$name = "鱼"; $age = "10086"; $sex = "保密"; $phone = "10010"; $result = compact("name","age","sex","phone");//注意:参数为你定义的变量名 不对应将无法获取 dump($result);
输出结果均为:
array(4) { ["name"] => string(3) "鱼" ["age"] => string(5) "10086" ["sex"] => string(6) "保密" ["phone"] => string(5) "10010" }
由此可见,代码简洁性大大提高了。

浙公网安备 33010602011771号