• PHP Video Tutorials

PHP - http_build_query() Function



http_build_query() function can generate a URL-encoded query string.

Syntax

string http_build_query( 
   mixed $query_data [, string $numeric_prefix [, string $arg_separator [, 
      int $enc_type = PHP_QUERY_RFC1738 ]]
   ] 
)

http_build_query() function can generate a URL-encoded query string from an associative (or indexed) array provided.

http_build_query() function can return a URL-encoded string.

Example

<?php
   $data = array(
      "foo" => "bar",
      "baz" => "boom",
      "cow" => "milk",
      "php" => "hypertext processor"
   );

   echo http_build_query($data) . "\n";
   echo http_build_query($data, '', '&');
?>

Example

<?php
   $data = array("foo", "bar", "baz", "boom", "cow" => "milk", "php" => "hypertext processor");

   echo http_build_query($data) . "\n";
   echo http_build_query($data, 'myvar_');
?>

Example

<?php
   $data = array(
      "user" => array(
         "name" => "Bob Smith",
         "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"
   );
   echo http_build_query($data, "flags_");
?>

Example

<?php
   class parentClass {
      public $pub = "publicParent";
      protected $prot = "protectedParent";
      private $priv = "privateParent";
      public $pub_bar = Null;
      protected $prot_bar = Null;
      private $priv_bar = Null;

      public function __construct(){
         $this->pub_bar  = new childClass();
         $this->prot_bar = new childClass();
         $this->priv_bar = new childClass();
      }
   }
   class childClass {
      public $pub = "publicChild";
      protected $prot = "protectedChild";
      private $priv = "privateChild";
   }
   $parent = new parentClass();
   echo http_build_query($parent);
?>
php_function_reference.htm
Advertisements