Free cookie consent management tool by TermsFeed Generator PHP compact | Amir Kamizi
AMIR KAMIZI
Home Blog Courses Books Newsletter Store Membership Buy me a coffee
PHP compact

PHP compact

Last Updated on Feb 14, 2023

Introduction

Compact is one of those functions that helps the readability of your code a lot and also reduces the lines of code you should write.

Compact

Compact gets the name of the variables as argument and then creates an array with name of the variable as the key and the value of the variable as the value of the array element.

 

So let’s say we have a few variables and we want to create an array from them. Without compact I should do something like this:

$twitter = 'pratham';
$feedhive = 'simon';
$php = 'amir';
$people = [
   'twitter'  => $twitter,
   'feedhive' => $feedhive,
   'php'      => $php,
];

But now thanks to compact I can write this

$people = compact('twitter','feedhive','php');
print_r($people);
// Array (
// [twitter]    => pratham
// [feedhive]   => simon
// [php]        => amir
// )

Isn’t it amazing? Using the compact function is going to help you a lot.

Conclusion

Now you know about compact function in PHP.

I recommend you to open a PHP files and try to define an array of variables with compact function.

If you have any suggestions, questions, or opinions, please contact me. I’m looking forward to hearing from you!

Key takeaways

  • compact function in php

Category: programming

Tags: #php #array

Join the Newsletter

Subscribe to get my latest content by email.

I won't send you spam. Unsubscribe at any time.

Related Posts

PHP Array Merge
Feb 14, 2023 programming

PHP Array Merge

Today we are going to talk about array merge function in PHP. This function is very useful and merges all the arrays that you pass as arguments and returns an array. ...

3 Min Read Read More
PHP Array Replace
Feb 14, 2023 programming

PHP Array Replace

Today we are going to talk about array replace function in PHP. Array replace is another array function that is very simple yet a very powerful function. ...

8 Min Read Read More
How to Combine Excel Files Using Python
May 08, 2024 programming

How to Combine Excel Files Using Python

Sometimes you might have many excel files that you need to combine, whether it's reports, user data or anything else, merging them into one file with Python can be easily done. ...

9 Min Read Read More
Node.js Beginner to Advanced 2025: Everything You Need to Know to Build Modern Projects
Jul 20, 2025 programming

Node.js Beginner to Advanced 2025: Everything You Need to Know to Build Modern Projects

Node.js has matured into one of the most essential tools in modern web development. Originally released in 2009, Node.js transformed JavaScript from a strictly frontend language into a robust backend solution capable of powering APIs, real-time applications, microservices, IoT systems, and more. ...

38 Min Read Read More

Recommended Courses

Introduction to Machine Learning in PHP

Introduction to Machine Learning in PHP

Learn to Build Different Machine Learning Models Easily ...

PHP Tutorial Beginner to Advanced

PHP Tutorial Beginner to Advanced

Learn everything you need to start a successful career as a PHP developer ...