PHP

PHP Menu

PHP

compact() Function - Definition, Syntax, Parameters, Examples

Definition

The compact() function creates an array from variables and their values.

Syntax

compact(varname1, varname2, ...)

Parameters

Parameter Description
varname1 Required. Can be a string representing variable name, or an array of variables.
varname2... Optional. Can be a string representing variable name, or an array of variables. Multiple parameters are allowed.

Example

<?php
$name = "Mark"; $age = 22; $country = "UK";
// Example 1 $result = compact("name", "age"); print_r($result); echo "<br>";
// Example 2 $person = array("name", "age"); $result = compact($person, "country"); print_r($result); echo "<br>";

Introduction

PHP Basics

PHP Advance

PHP OOP

PHP Functions and Methods