Home > Article > Backend Development > How to use compact function
PHP compact() function is used to create an array, including variable names and their values.
php compact() function syntax
Function: Create an array containing variable names and their values
Syntax:
compact(var1,var2...)
Parameters:
var1 Required. Can be a string with a variable name, or an array of variables.
var2,... Optional. Can be a string with a variable name, or an array of variables. Multiple parameters are allowed.
Description: Create an array containing variable names and their values. Any string that does not have a corresponding variable name is ignored.
php compact() function example 1
<?php $firstname = "西门"; $lastname = "灭绝"; $age = "25"; $result = compact("firstname", "lastname", "age"); print_r($result); ?>
Output:
Array ( [firstname] => 西门 [lastname] => 灭绝 [age] => 25 )
php compact() function example 2
<?php $name = "无忌"; $job = "讲师"; $age = "20"; $result = compact("name", "job", "age"); print_r($result); ?>
Output:
Array ( [name] => 无忌 [job] => 讲师 [age] => 20 )
This article is an introduction to the PHP compact function. I hope it will be helpful to friends in need!
The above is the detailed content of How to use compact function. For more information, please follow other related articles on the PHP Chinese website!