- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
compact() function in PHP
The compact() function creates an array containing variables and their values.. It returns an array with all the variables added to it
Syntax
compact(variable1, variable2)
Parameters
variable1 − Can be a string with the variable name, or an array of variables. Required.
variable2 − Can be a string with the variable name, or an array of variables. Optional.
Return
The compact() function returns an array with all the variables added to it.
Example
The following is an example −
<?php $ELE = "Electronics"; $ACC = "Accessories"; $res = compact("ELE", "ACC"); print_r($res); ?>
Output
The following is the output −
Array ( [ELE] => Electronics [ACC] => Accessories )
Example
Let us see another example −
<?php $name = "Tom"; $subject = "Hanks"; $id = "001"; $details = array("name", "subject"); $res = compact($details, "id"); print_r($res); ?>
Output
The following is the output −
Array ( [name => Tom [subject] => Hanks [id] => 001 )
Advertisements
To Continue Learning Please Login
Login with Google