據我目前所知,這個內置函式主要作用是 可以讓你的代碼更清晰,節省代碼量
我們先看一下正常的寫法:
$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" }
由此可見,代碼簡潔性大大提高了,
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/59829.html
標籤:PHP
下一篇:Composer 設定為全域
