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 Conditionals
Mar 21, 2023 programming

PHP Conditionals

Today we are going to learn about conditionals in PHP. Conditionals are very useful in any programming language. So important that we can argue that the foundation for all the technologies around us are conditions. ...

7 Min Read Read More
PHP Array Sort
Feb 14, 2023 programming

PHP Array Sort

Today we are going to talk about sorting arrays in PHP. we will take a look at some of the most useful and common array sort functions in PHP and compare their functionality. ...

7 Min Read Read More
PHP Array Intersect
Feb 14, 2023 programming

PHP Array Intersect

Today we are going to talk about array intersect functions in PHP .If you want to find the items in your array that are present in the other arrays, array intersect is your friend. ...

5 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

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 ...