Home > Backend Development > PHP Tutorial > How to use compact function

How to use compact function

藏色散人
Release: 2023-04-05 07:06:01
Original
2568 people have browsed it

PHP compact() function is used to create an array, including variable names and their values.

How to use compact function

php compact() function syntax

Function: Create an array containing variable names and their values

Syntax:

compact(var1,var2...)
Copy after login

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);
?>
Copy after login

Output:

Array ( [firstname] => 西门 [lastname] => 灭绝 [age] => 25 )
Copy after login

php compact() function example 2

<?php
$name = "无忌";
$job = "讲师";
$age = "20";
$result = compact("name", "job", "age");
print_r($result);
?>
Copy after login

Output:

Array ( [name] => 无忌 [job] => 讲师 [age] => 20 )
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template