PHP code example of fabgg / url-search-params

1. Go to this page and download the library: Download fabgg/url-search-params library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

fabgg / url-search-params example snippets




// Include dependencies installed with composer
 new URLSearchParams('?q=github');
$search->merge(['user'=>'fabrice'])
echo $search->toString();



$search = new URLSearchParams('?q=github+web+site&u=yan');
// equivalent to:
$search = new URLSearchParams(['q' => 'github web site', 'u' => 'yan']);


$search= new URLSearchParams();
// array
search->append(["id"=> 1]);

$search = new URLSearchParams();
// key and value
$search->appendTp("id"=> 1);
// value can be an array
$search->appendTp("id"=> [3,5,7]);
echo (string)$search;
// result is `?id=1&id=3&id=5&id=7`


$search.delete("id");

$search.get("id");
// return an array [1, 3, 5, 7]

$search = new URLSearchParams('?q=github&u=yan');
// all query parameters are
$search.getAll();
// return ['q' => ['github'], 'u' => ['yan']]
$search.getAll("q");
// return ['q' => ['github']] like $search->get('q')


$search.has("id");

$search = new URLSearchParams('?q=vegetable&flavour=sweet');
$search->merge('?q=fruits');
echo $search->toString();
// return '?q=vegetable&q=fruits&flavour=sweet'
$search->merge(['flavour' => 'bitter','color'=>'red tomato']);
echo $search->toString();
// return '?q=vegetable&q=fruits&flavour=sweet&flavour=bitter&color=red+tomato'


$search.toString();
// or call __toString()
(string)$search;

$search = new URLSearchParams('?q=github&u=yan');
$search->keys();
// return ['q','u']