<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.array.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'en',
  ),
  'this' => 
  array (
    0 => 'function.compact.php',
    1 => 'compact',
    2 => 'Create array containing variables and their values',
  ),
  'up' => 
  array (
    0 => 'ref.array.php',
    1 => 'Array Functions',
  ),
  'prev' => 
  array (
    0 => 'function.asort.php',
    1 => 'asort',
  ),
  'next' => 
  array (
    0 => 'function.count.php',
    1 => 'count',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/array/functions/compact.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.compact" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">compact</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">compact</span> &mdash; <span class="dc-title">Create array containing variables and their values</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.compact-description">
  <h3 class="title">Description</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>compact</strong></span>(<span class="methodparam"><span class="type"><span class="type"><a href="language.types.array.php" class="type array">array</a></span>|<span class="type"><a href="language.types.string.php" class="type string">string</a></span></span> <code class="parameter">$var_name</code></span>, <span class="methodparam"><span class="type"><span class="type"><a href="language.types.array.php" class="type array">array</a></span>|<span class="type"><a href="language.types.string.php" class="type string">string</a></span></span> <code class="parameter">...$var_names</code></span>): <span class="type"><a href="language.types.array.php" class="type array">array</a></span></div>

  <p class="para rdfs-comment">
   Creates an array containing variables and their values.
  </p>
  <p class="para">
   For each of these, <span class="function"><strong>compact()</strong></span> looks for a
   variable with that name in the current
   <a href="features.gc.refcounting-basics.php" class="link">symbol table</a>
   and adds it to the output array such that the variable name becomes the key
   and the contents of the variable become the value for that key.
   In short, it does the opposite of <span class="function"><a href="function.extract.php" class="function">extract()</a></span>.
  </p>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    Before PHP 7.3, any strings that are not set will silently be skipped.
   </p>
  </p></blockquote> 
 </div>

 <div class="refsect1 parameters" id="refsect1-function.compact-parameters">
  <h3 class="title">Parameters</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">var_name</code></dt>
     <dt><code class="parameter">var_names</code></dt>
     <dd>
      <p class="para">
       <span class="function"><strong>compact()</strong></span> 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.  The array can contain other
       arrays of variable names inside it; <span class="function"><strong>compact()</strong></span>
       handles it recursively.
      </p>
     </dd>
    
   </dl>
  </p>
 </div>

 <div class="refsect1 returnvalues" id="refsect1-function.compact-returnvalues">
  <h3 class="title">Return Values</h3>
  <p class="para">
   Returns the output array with all the variables added to it.
  </p>
 </div>


 <div class="refsect1 errors" id="refsect1-function.compact-errors">
  <h3 class="title">Errors/Exceptions</h3>
  <p class="para">
   <span class="function"><strong>compact()</strong></span> issues an <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong> level error if a given string
   refers to an unset variable.
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.compact-changelog">
  <h3 class="title">Changelog</h3>
  <table class="doctable informaltable">
   
    <thead>
     <tr>
      <th>Version</th>
      <th>Description</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr>
      <td>8.0.0</td>
      <td>
       If a given string references an unset variable, an <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong> level error is now issued.
      </td>
     </tr>

     <tr>
      <td>7.3.0</td>
      <td>
       <span class="function"><strong>compact()</strong></span> now issues an <strong><code><a href="errorfunc.constants.php#constant.e-notice">E_NOTICE</a></code></strong> level error if a given string
       refers to an unset variable. Formerly, such strings have been silently skipped.
      </td>
     </tr>

    </tbody>
   
  </table>

 </div>


 <div class="refsect1 examples" id="refsect1-function.compact-examples">
  <h3 class="title">Examples</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>Example #1 <span class="function"><strong>compact()</strong></span> example</strong></p>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br />$city  </span><span style="color: #007700">= </span><span style="color: #DD0000">"San Francisco"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$state </span><span style="color: #007700">= </span><span style="color: #DD0000">"CA"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$event </span><span style="color: #007700">= </span><span style="color: #DD0000">"SIGGRAPH"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$location_vars </span><span style="color: #007700">= array(</span><span style="color: #DD0000">"city"</span><span style="color: #007700">, </span><span style="color: #DD0000">"state"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$result </span><span style="color: #007700">= </span><span style="color: #0000BB">compact</span><span style="color: #007700">(</span><span style="color: #DD0000">"event"</span><span style="color: #007700">, </span><span style="color: #0000BB">$location_vars</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$result</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>The above example will output:</p></div>
    <div class="example-contents screen">
<div class="annotation-interactive examplescode"><pre class="examplescode">Array
(
    [event] =&gt; SIGGRAPH
    [city] =&gt; San Francisco
    [state] =&gt; CA
)</pre>
</div>
    </div>
   </div>
  </p>
 </div>

 <div class="refsect1 notes" id="refsect1-function.compact-notes">
  <h3 class="title">Notes</h3>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <strong>Gotcha</strong><br />
   <p class="para">
    Because <a href="language.variables.variable.php" class="link">variable
    variables</a> may not be used with PHP&#039;s
    <a href="language.variables.superglobals.php" class="link">Superglobal
    arrays</a> within functions, the Superglobal arrays may not be passed
    into <span class="function"><strong>compact()</strong></span>.
   </p>
  </p></blockquote>
 </div>

 <div class="refsect1 seealso" id="refsect1-function.compact-seealso">
  <h3 class="title">See Also</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="function.extract.php" class="function" rel="rdfs-seeAlso">extract()</a> - Import variables into the current symbol table from an array</span></li>
   </ul>
  </p>
 </div>

</div><?php manual_footer($setup); ?>