Vous êtes sur la page 1sur 3

4/21/2016

PHP:http_build_queryManual

Scrolltobottom
gg
Scrolltotop
gh
Gotohomepage
gs
Gotosearch
(currentpage)
/
Focussearchbox
parse_url
get_meta_tags
PHPManual
FunctionReference
OtherBasicExtensions
URLs
URLFunctions
Changelanguage: English
EditReportaBug

http_build_query
(PHP5,PHP7)
http_build_queryGenerateURLencodedquerystring

Description
stringhttp_build_query(mixed$query_data[,string$numeric_prefix[,string$arg_separator[,int
$enc_type=PHP_QUERY_RFC1738]]])
GeneratesaURLencodedquerystringfromtheassociative(orindexed)arrayprovided.

Parameters
query_data

Maybeanarrayorobjectcontainingproperties.
Ifquery_dataisanarray,itmaybeasimpleonedimensionalstructure,oranarrayofarrays
(whichinturnmaycontainotherarrays).
Ifquery_dataisanobject,thenonlypublicpropertieswillbeincorporatedintotheresult.
numeric_prefix

http://php.net/manual/en/function.httpbuildquery.php

3/13

4/21/2016

PHP:http_build_queryManual

Ifnumericindicesareusedinthebasearrayandthisparameterisprovided,itwillbeprependedto
thenumericindexforelementsinthebasearrayonly.
ThisismeanttoallowforlegalvariablenameswhenthedataisdecodedbyPHPoranotherCGI
applicationlateron.
arg_separator

arg_separator.outputisusedtoseparateargumentsbutmaybeoverriddenbyspecifyingthis
parameter.
enc_type

Bydefault,PHP_QUERY_RFC1738.
Ifenc_typeisPHP_QUERY_RFC1738,thenencodingisperformedperRFC1738andthe
application/xwwwformurlencodedmediatype,whichimpliesthatspacesareencodedasplus(+)
signs.
Ifenc_typeisPHP_QUERY_RFC3986,thenencodingisperformedaccordingtoRFC3986,and
spaceswillbepercentencoded(%20).

ReturnValues
ReturnsaURLencodedstring.

Changelog
Version
Description
5.4.0
Theenc_typeparameterwasadded.
5.1.3
Squarebracketsareescaped.
5.1.2
Thearg_separatorparameterwasadded.

Examples
Example#1Simpleusageofhttp_build_query()
<?php
$data=array('foo'=>'bar',
'baz'=>'boom',
'cow'=>'milk',
'php'=>'hypertextprocessor');
echohttp_build_query($data)."\n";
echohttp_build_query($data,'','&amp;');
?>

Theaboveexamplewilloutput:
http://php.net/manual/en/function.httpbuildquery.php

4/13

4/21/2016

PHP:http_build_queryManual

foo=bar&baz=boom&cow=milk&php=hypertext+processor
foo=bar&amp;baz=boom&amp;cow=milk&amp;php=hypertext+processor

Example#2http_build_query()withnumericallyindexelements.
<?php
$data=array('foo','bar','baz','boom','cow'=>'milk','php'=>'hypertextprocessor');
echohttp_build_query($data)."\n";
echohttp_build_query($data,'myvar_');
?>

Theaboveexamplewilloutput:
0=foo&1=bar&2=baz&3=boom&cow=milk&php=hypertext+processor
myvar_0=foo&myvar_1=bar&myvar_2=baz&myvar_3=boom&cow=milk&php=hypertext+processor

Example#3http_build_query()withcomplexarrays
<?php
$data=array('user'=>array('name'=>'BobSmith',
'age'=>47,
'sex'=>'M',
'dob'=>'5/12/1956'),
'pastimes'=>array('golf','opera','poker','rap'),
'children'=>array('bobby'=>array('age'=>12,
'sex'=>'M'),
'sally'=>array('age'=>8,
'sex'=>'F')),
'CEO');
echohttp_build_query($data,'flags_');
?>

thiswilloutput:(wordwrappedforreadability)
user%5Bname%5D=Bob+Smith&user%5Bage%5D=47&user%5Bsex%5D=M&
user%5Bdob%5D=5%2F12%2F1956&pastimes%5B0%5D=golf&pastimes%5B1%5D=opera&
pastimes%5B2%5D=poker&pastimes%5B3%5D=rap&children%5Bbobby%5D%5Bage%5D=12&
children%5Bbobby%5D%5Bsex%5D=M&children%5Bsally%5D%5Bage%5D=8&
children%5Bsally%5D%5Bsex%5D=F&flags_0=CEO

Note:
Onlythenumericallyindexedelementinthebasearray"CEO"receivedaprefix.Theother
numericindices,foundunderpastimes,donotrequireastringprefixtobelegalvariable
names.
Example#4Usinghttp_build_query()withanobject
<?php
classparentClass{
public$pub='publicParent';
http://php.net/manual/en/function.httpbuildquery.php

5/13

Vous aimerez peut-être aussi