Thursday, October 29, 2020

PHP compact() Function

PHP compact() Function

Hello dear readers! welcome back to another section of our tutorial on PHP. In this tutorial guide, we are going to be studying about the PHP compact() Function.

PHP compact() function takes a variable number of parameters. Each parameter can be either a string containing the name of the variable, or an array of variable names.


Following below is the syntax to use this function -

compact($var1, $var2...);

Parameter Details

Sr.NoParameter & Description


It can be a string with the variable name, or an array of variables.



It can be a string with the variable name, or an array of variables.

Return Value

It returns the output array with all the variables added to it.


Try out the below example -

   $city  = "Lekki";
   $state = "LAG";
   $event = "New Year";
   $result = compact("city", "state", "event");


When the above code is executed, it will produce the following result -

Array ( [city] => Lekki [state] => LAG [event] => New Year )

Alright guys! This is where we are rounding up for this tutorial post. In our next tutorial guide, we are going to be discussing about the PHP count() Function.

Feel free to ask your questions where necessary and i will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.


Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.