// Value to be passed to the next page
$search = 'hello world!';

// Encode the value for use in a query string
$encodedSearch = urlencode($search);

// Append the encoded value to the URL
$url = 'https://example.com/search.php?q=' . $encodedSearch;

echo $url;
// Output: 'https://example.com/search.php?q=hello+world%21'
urlencode(string $string): string
$userinput = 'Data123!@-_ +';
echo "UserInput: $userinput\n";
echo '<a href="mycgi?foo=' . urlencode($userinput) . '">';

/* Output:
'UserInput: Data123!@-_ +'
'<a href="mycgi?foo=Data123%21%40-_+%2B">'
*/
$name = 'John Smith';
$age = 20;
$query_string = 'name=' . urlencode($name) . '&age=' . urlencode($age);

$url = "https://example.com/?$query_string";
echo $url;
// Output: 'https://example.com/?name=John+Smith&age=20'