Q. Explain http_build_query() in php.
Answer:
The http_build_query() function in PHP is used to generate a URL-encoded string from an associative array or an object. This function is commonly used to build query strings that are appended to URLs in HTTP requests.
Here's the basic syntax of http_build_query():
string http_build_query ( mixed $query_data, string $numeric_prefix = '', string $arg_separator = '', int $enc_type = PHP_QUERY_RFC3986 )
$query_data: The associative array or object to be converted into a URL-encoded string.
$numeric_prefix: Optional. If numeric indices should be prefixed with this string.
$arg_separator: Optional. Specifies the string to use as a separator between variables in the resulting string. The default is "&".
$enc_type: Optional. Specifies how to handle spaces. The default (PHP_QUERY_RFC3986) is recommended for most cases.
Here's a simple example:
<?php
$data = array(
'name' => 'John Doe',
'age' => 30,
'city' => 'Example City'
);
$queryString = http_build_query($data);
echo $queryString;
?>
In this example, the $data array is converted into a URL-encoded string using http_build_query(). The resulting string will be something like:
name=John+Doe&age=30&city=Example+City
This can be useful when constructing URLs for GET requests, especially when dealing with forms or API requests.
You can also use http_build_query() with nested arrays or objects to create more complex query strings. Additionally, the function is often used in combination with functions like urlencode() to handle special characters or complex data structures.
<?php
$data = array(
'name' => 'John Doe',
'age' => 30,
'address' => array(
'street' => '123 Main St',
'city' => 'Example City'
)
);
$queryString = http_build_query($data);
echo $queryString;
?>
This would produce a query string like:
name=John+Doe&age=30&address%5Bstreet%5D=123+Main+St&address%5Bcity%5D=Example+City
Remember that the resulting string is suitable for use in a URL, and it can be appended to a URL as a query string in an HTTP request.
js
Friday, December 15, 2023
Explain http_build_query() in php.
Subscribe to:
Post Comments (Atom)
-
ভাৰতৰ ৰাজনৈতিক দল অনুশীলনীৰ প্রশ্নোত্তৰ অতি চমু প্রশ্নোত্তৰ প্রশ্ন ১। একদলীয় শাসন ব্যৱস্থা থকা এখন দেশৰ নাম লিখা। উত্তৰঃ চীন। প...
-
উদ্যান শস্যৰ পৰিচয় পৰিচয় উদ্যান শস্য এটা বিজ্ঞান , লগতে , উদ্যান শস্য , যেনে ফল - মূল আৰু শাক - পাচলি , ...
No comments:
Post a Comment