<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.url.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'uk',
  ),
  'this' => 
  array (
    0 => 'function.http-build-query.php',
    1 => 'http_build_query',
    2 => 'Generate URL-encoded query string',
  ),
  'up' => 
  array (
    0 => 'ref.url.php',
    1 => 'URL Функції',
  ),
  'prev' => 
  array (
    0 => 'function.get-meta-tags.php',
    1 => 'get_meta_tags',
  ),
  'next' => 
  array (
    0 => 'function.parse-url.php',
    1 => 'parse_url',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/url/functions/http-build-query.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.http-build-query" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">http_build_query</h1>
  <p class="verinfo">(PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">http_build_query</span> &mdash; <span class="dc-title">Generate URL-encoded query string</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.http-build-query-description">
  <h3 class="title">Опис</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>http_build_query</strong></span>(<br>&nbsp;&nbsp;&nbsp;&nbsp;<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.object.php" class="type object">object</a></span></span> <code class="parameter">$data</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$numeric_prefix</code><span class="initializer"> = &quot;&quot;</span></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><span class="type"><a href="language.types.null.php" class="type null">?</a></span><span class="type"><a href="language.types.string.php" class="type string">string</a></span></span> <code class="parameter">$arg_separator</code><span class="initializer"> = <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong></span></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span> <code class="parameter">$encoding_type</code><span class="initializer"> = <strong><code><a href="url.constants.php#constant.php-query-rfc1738">PHP_QUERY_RFC1738</a></code></strong></span></span><br>): <span class="type"><a href="language.types.string.php" class="type string">string</a></span></div>

  <p class="para rdfs-comment">
   Generates a URL-encoded query string from the associative (or indexed)
   array provided.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.http-build-query-parameters">
  <h3 class="title">Параметри</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">data</code></dt>
     <dd>
      <p class="para">
       May be an array or object containing properties.
      </p>
      <p class="para">
       If <code class="parameter">data</code> is an array, it may be a simple
       one-dimensional structure, or an array of arrays (which in
       turn may contain other arrays).
      </p>
      <p class="para">
       If <code class="parameter">data</code> is an object, then only public
       properties will be incorporated into the result.
      </p>
     </dd>
    
    
     <dt><code class="parameter">numeric_prefix</code></dt>
     <dd>
      <p class="para">
       If numeric indices are used in the base array and this parameter is
       provided, it will be prepended to the numeric index for elements in
       the base array only.
      </p>
      <p class="para">
       This is meant to allow for legal variable names when the data is
       decoded by PHP or another CGI application later on.
      </p>
     </dd>
    
    
     <dt><code class="parameter">arg_separator</code></dt>
     <dd>
      <p class="para">
       The argument separator. If not set or <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong>, 
       <a href="ini.core.php#ini.arg-separator.output" class="link">arg_separator.output</a>
       is used to separate arguments.
      </p>
     </dd>
    
    
     <dt><code class="parameter">encoding_type</code></dt>
     <dd>
      <p class="para">
       By default, <strong><code><a href="url.constants.php#constant.php-query-rfc1738">PHP_QUERY_RFC1738</a></code></strong>.
      </p>
      <p class="para">
       If <code class="parameter">encoding_type</code> is
       <strong><code><a href="url.constants.php#constant.php-query-rfc1738">PHP_QUERY_RFC1738</a></code></strong>, then encoding is performed per
       <a href="https://datatracker.ietf.org/doc/html/rfc1738" class="link external">&raquo;&nbsp;RFC 1738</a> and the
       <code class="literal">application/x-www-form-urlencoded</code> media type, which
       implies that spaces are encoded as plus (<code class="literal">+</code>) signs.
      </p>
      <p class="para">
       If <code class="parameter">encoding_type</code> is
       <strong><code><a href="url.constants.php#constant.php-query-rfc3986">PHP_QUERY_RFC3986</a></code></strong>, then encoding is performed
       according to <a href="https://datatracker.ietf.org/doc/html/rfc3986" class="link external">&raquo;&nbsp;RFC 3986</a>, and
       spaces will be percent encoded (<code class="literal">%20</code>).
      </p>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.http-build-query-returnvalues">
  <h3 class="title">Значення, що повертаються</h3>
  <p class="para">
   Returns a URL-encoded string.
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.http-build-query-changelog">
  <h3 class="title">Журнал змін</h3>
  <table class="doctable informaltable">
   
    <thead>
     <tr>
      <th>Версія</th>
      <th>Опис</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr>
      <td>8.0.0</td>
      <td>
       <code class="parameter">arg_separator</code> is now nullable.
      </td>
     </tr>

    </tbody>
   
  </table>

 </div>


 <div class="refsect1 examples" id="refsect1-function.http-build-query-examples">
  <h3 class="title">Приклади</h3>
  <div class="example" id="example-1">
   <p><strong>Приклад #1 Simple usage of <span class="function"><strong>http_build_query()</strong></span></strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$data </span><span style="color: #007700">= array(<br />    </span><span style="color: #DD0000">'foo' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'bar'</span><span style="color: #007700">,<br />    </span><span style="color: #DD0000">'baz' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'boom'</span><span style="color: #007700">,<br />    </span><span style="color: #DD0000">'cow' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'milk'</span><span style="color: #007700">,<br />    </span><span style="color: #DD0000">'null' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">null</span><span style="color: #007700">,<br />    </span><span style="color: #DD0000">'php' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'hypertext processor'<br /></span><span style="color: #007700">);<br /><br />echo </span><span style="color: #0000BB">http_build_query</span><span style="color: #007700">(</span><span style="color: #0000BB">$data</span><span style="color: #007700">) . </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />echo </span><span style="color: #0000BB">http_build_query</span><span style="color: #007700">(</span><span style="color: #0000BB">$data</span><span style="color: #007700">, </span><span style="color: #DD0000">''</span><span style="color: #007700">, </span><span style="color: #DD0000">'&amp;amp;'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

   <div class="example-contents"><p>Поданий вище приклад
виведе:</p></div>
   <div class="example-contents screen">
<div class="examplescode"><pre class="examplescode">foo=bar&amp;baz=boom&amp;cow=milk&amp;php=hypertext+processor
foo=bar&amp;amp;baz=boom&amp;amp;cow=milk&amp;amp;php=hypertext+processor</pre>
</div>
   </div>
  </div>

  <div class="example" id="example-2">
   <p><strong>Приклад #2 <span class="function"><strong>http_build_query()</strong></span> with numerically index elements.</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$data </span><span style="color: #007700">= array(</span><span style="color: #DD0000">'foo'</span><span style="color: #007700">, </span><span style="color: #DD0000">'bar'</span><span style="color: #007700">, </span><span style="color: #DD0000">'baz'</span><span style="color: #007700">, </span><span style="color: #0000BB">null</span><span style="color: #007700">, </span><span style="color: #DD0000">'boom'</span><span style="color: #007700">, </span><span style="color: #DD0000">'cow' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'milk'</span><span style="color: #007700">, </span><span style="color: #DD0000">'php' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'hypertext processor'</span><span style="color: #007700">);<br /><br />echo </span><span style="color: #0000BB">http_build_query</span><span style="color: #007700">(</span><span style="color: #0000BB">$data</span><span style="color: #007700">) . </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />echo </span><span style="color: #0000BB">http_build_query</span><span style="color: #007700">(</span><span style="color: #0000BB">$data</span><span style="color: #007700">, </span><span style="color: #DD0000">'myvar_'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

   <div class="example-contents"><p>Поданий вище приклад
виведе:</p></div>
   <div class="example-contents screen">
<div class="examplescode"><pre class="examplescode">0=foo&amp;1=bar&amp;2=baz&amp;4=boom&amp;cow=milk&amp;php=hypertext+processor
myvar_0=foo&amp;myvar_1=bar&amp;myvar_2=baz&amp;myvar_4=boom&amp;cow=milk&amp;php=hypertext+processor</pre>
</div>
   </div>
  </div>
  <div class="example" id="example-3">
   <p><strong>Приклад #3 <span class="function"><strong>http_build_query()</strong></span> with complex arrays</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$data </span><span style="color: #007700">= array(<br />    </span><span style="color: #DD0000">'user' </span><span style="color: #007700">=&gt; array(<br />        </span><span style="color: #DD0000">'name' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'Bob Smith'</span><span style="color: #007700">,<br />        </span><span style="color: #DD0000">'age'  </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">47</span><span style="color: #007700">,<br />        </span><span style="color: #DD0000">'sex'  </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'M'</span><span style="color: #007700">,<br />        </span><span style="color: #DD0000">'dob'  </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'5/12/1956'<br />    </span><span style="color: #007700">),<br />    </span><span style="color: #DD0000">'pastimes' </span><span style="color: #007700">=&gt; array(</span><span style="color: #DD0000">'golf'</span><span style="color: #007700">, </span><span style="color: #DD0000">'opera'</span><span style="color: #007700">, </span><span style="color: #DD0000">'poker'</span><span style="color: #007700">, </span><span style="color: #DD0000">'rap'</span><span style="color: #007700">),<br />    </span><span style="color: #DD0000">'children' </span><span style="color: #007700">=&gt; array(<br />        </span><span style="color: #DD0000">'bobby' </span><span style="color: #007700">=&gt; array(</span><span style="color: #DD0000">'age'</span><span style="color: #007700">=&gt;</span><span style="color: #0000BB">12</span><span style="color: #007700">, </span><span style="color: #DD0000">'sex'</span><span style="color: #007700">=&gt;</span><span style="color: #DD0000">'M'</span><span style="color: #007700">),<br />        </span><span style="color: #DD0000">'sally' </span><span style="color: #007700">=&gt; array(</span><span style="color: #DD0000">'age'</span><span style="color: #007700">=&gt;</span><span style="color: #0000BB">8</span><span style="color: #007700">, </span><span style="color: #DD0000">'sex'</span><span style="color: #007700">=&gt;</span><span style="color: #DD0000">'F'</span><span style="color: #007700">)<br />    ),<br />    </span><span style="color: #DD0000">'CEO'<br /></span><span style="color: #007700">);<br /><br />echo </span><span style="color: #0000BB">http_build_query</span><span style="color: #007700">(</span><span style="color: #0000BB">$data</span><span style="color: #007700">, </span><span style="color: #DD0000">'flags_'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

   <div class="example-contents"><p>
    The above example will output: (word wrapped for readability)
   </p></div>
   <div class="example-contents screen">
<div class="examplescode"><pre class="examplescode">user%5Bname%5D=Bob+Smith&amp;user%5Bage%5D=47&amp;user%5Bsex%5D=M&amp;
user%5Bdob%5D=5%2F12%2F1956&amp;pastimes%5B0%5D=golf&amp;pastimes%5B1%5D=opera&amp;
pastimes%5B2%5D=poker&amp;pastimes%5B3%5D=rap&amp;children%5Bbobby%5D%5Bage%5D=12&amp;
children%5Bbobby%5D%5Bsex%5D=M&amp;children%5Bsally%5D%5Bage%5D=8&amp;
children%5Bsally%5D%5Bsex%5D=F&amp;flags_0=CEO</pre>
</div>
   </div>
   <div class="example-contents"><p>
    <blockquote class="note"><p><strong class="note">Зауваження</strong>: 
     <p class="para">
      Only the numerically indexed element in the base array &quot;CEO&quot; received a
      prefix.  The other numeric indices, found under pastimes, do not
      require a string prefix to be legal variable names.
     </p>
    </p></blockquote>
   </p></div>
  </div>

  <div class="example" id="example-4">
   <p><strong>Приклад #4 Using <span class="function"><strong>http_build_query()</strong></span> with an object</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class </span><span style="color: #0000BB">parentClass </span><span style="color: #007700">{<br />    public    </span><span style="color: #0000BB">$pub      </span><span style="color: #007700">= </span><span style="color: #DD0000">'publicParent'</span><span style="color: #007700">;<br />    protected </span><span style="color: #0000BB">$prot     </span><span style="color: #007700">= </span><span style="color: #DD0000">'protectedParent'</span><span style="color: #007700">;<br />    private   </span><span style="color: #0000BB">$priv     </span><span style="color: #007700">= </span><span style="color: #DD0000">'privateParent'</span><span style="color: #007700">;<br />    public    </span><span style="color: #0000BB">$pub_bar  </span><span style="color: #007700">= </span><span style="color: #0000BB">null</span><span style="color: #007700">;<br />    protected </span><span style="color: #0000BB">$prot_bar </span><span style="color: #007700">= </span><span style="color: #0000BB">null</span><span style="color: #007700">;<br />    private   </span><span style="color: #0000BB">$priv_bar </span><span style="color: #007700">= </span><span style="color: #0000BB">null</span><span style="color: #007700">;<br /><br />    public function </span><span style="color: #0000BB">__construct</span><span style="color: #007700">(){<br />        </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">pub_bar  </span><span style="color: #007700">= new </span><span style="color: #0000BB">childClass</span><span style="color: #007700">();<br />        </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">prot_bar </span><span style="color: #007700">= new </span><span style="color: #0000BB">childClass</span><span style="color: #007700">();<br />        </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">priv_bar </span><span style="color: #007700">= new </span><span style="color: #0000BB">childClass</span><span style="color: #007700">();<br />    }<br />}<br /><br />class </span><span style="color: #0000BB">childClass </span><span style="color: #007700">{<br />    public    </span><span style="color: #0000BB">$pub  </span><span style="color: #007700">= </span><span style="color: #DD0000">'publicChild'</span><span style="color: #007700">;<br />    protected </span><span style="color: #0000BB">$prot </span><span style="color: #007700">= </span><span style="color: #DD0000">'protectedChild'</span><span style="color: #007700">;<br />    private   </span><span style="color: #0000BB">$priv </span><span style="color: #007700">= </span><span style="color: #DD0000">'privateChild'</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">$parent </span><span style="color: #007700">= new </span><span style="color: #0000BB">parentClass</span><span style="color: #007700">();<br /><br />echo </span><span style="color: #0000BB">http_build_query</span><span style="color: #007700">(</span><span style="color: #0000BB">$parent</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

   <div class="example-contents"><p>Поданий вище приклад
виведе:</p></div>
   <div class="example-contents screen">
<div class="examplescode"><pre class="examplescode">pub=publicParent&amp;pub_bar%5Bpub%5D=publicChild</pre>
</div>
   </div>
  </div>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.http-build-query-seealso">
  <h3 class="title">Прогляньте також</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="function.parse-str.php" class="function" rel="rdfs-seeAlso">parse_str()</a> - Parse a string as a URL query string</span></li>
    <li><span class="function"><a href="function.parse-url.php" class="function" rel="rdfs-seeAlso">parse_url()</a> - Parse a URL and return its components</span></li>
    <li><span class="function"><a href="function.urlencode.php" class="function" rel="rdfs-seeAlso">urlencode()</a> - URL-encodes string</span></li>
    <li><span class="function"><a href="function.array-walk.php" class="function" rel="rdfs-seeAlso">array_walk()</a> - Apply a user supplied function to every member of an array</span></li>
   </ul>
  </p>
 </div>


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