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"
}

由此可见,代码简洁性大大提高了。
今天刚接触到,如有不对,请大神指教!!!

猜你喜欢

转载自www.cnblogs.com/cheatingcat/p/12660842.html
今日推荐