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

How to use Git Alias to increase your productivity (video)
Nov 27, 2022 programming

How to use Git Alias to increase your productivity (video)

Today we are going to talk about Git Alias and how to increase your productivity by adding shorter aliases for commands that we use everyday. ...

8 Min Read Read More
PHP Date and Time
Mar 21, 2023 programming

PHP Date and Time

Today we are going to learn about date and time in PHP. knowing how to work with dates and times and how to work with different formats of dates is very important. ...

14 Min Read Read More
PHP Regular Expressions
Mar 22, 2023 programming

PHP Regular Expressions

Today we are going to learn about regular expressions in PHP. A regular expression is a sequence of characters that specifies a search pattern. it's important to know how to apply them in PHP. ...

5 Min Read Read More
PHP Simple Tinker Like Script
Feb 15, 2023 programming

PHP Simple Tinker Like Script

Today we are going to build a simple Tinker in PHP. Tinker is a tool that allows users to interact with the application through the command line. ...

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