PHP กระชับ ()ฟังก์ชั่น
ตัวอย่าง
สร้างอาร์เรย์จากตัวแปรและค่าของพวกมัน:
<?php
$firstname = "Peter";
$lastname = "Griffin";
$age = "41";
$result = compact("firstname", "lastname", "age");
print_r($result);
?>
ความหมายและการใช้งาน
ฟังก์ชั่น compact() สร้างอาร์เรย์จากตัวแปรและค่าของตัวแปร
หมายเหตุ:สตริงใดๆ ที่ไม่ตรงกับชื่อตัวแปรจะถูกข้ามไป
ไวยากรณ์
compact(var1, var2...)
ค่าพารามิเตอร์
Parameter | Description |
---|---|
var1 | Required. Can be a string with the variable name, or an array of variables |
var2,... | Optional. Can be a string with the variable name, or an array of variables. Multiple parameters are allowed. |
รายละเอียดทางเทคนิค
คืนมูลค่า: | ส่งกลับอาร์เรย์ที่มีตัวแปรทั้งหมดเพิ่มเข้าไป |
---|---|
รุ่น PHP: | 4+ |
บันทึกการเปลี่ยนแปลง: | ในเวอร์ชัน 7.3 ฟังก์ชันนี้จะแสดงข้อผิดพลาดระดับ E_NOTICE หากได้รับตัวแปรที่ไม่ได้ตั้งค่า |
ตัวอย่างเพิ่มเติม
ตัวอย่าง
การใช้สตริงที่ไม่ตรงกับตัวแปรและอาร์เรย์ของชื่อตัวแปร:
<?php
$firstname = "Peter";
$lastname = "Griffin";
$age = "41";
$name = array("firstname", "lastname");
$result = compact($name, "location", "age");
print_r($result);
?>
❮ การอ้างอิงอาร์เรย์ PHP