$data = "This is a sample string."; $urlEncoded = urlencode($data); echo $urlEncoded; // Output: This+is+a+sample+string%2E
$productId = 1234; $productName = "Widget A"; $queryString = "productId=" . urlencode($productId) . "&productName=" . urlencode($productName); $url = "http://example.com?" . $queryString; echo $url; // Output: http://example.com?productId=1234&productName=Widget+AIn the example above, we have two variables `$productId` and `$productName`. The values of these variables are used to create a query string that is then appended to the base URL `http://example.com`. The `urlencode()` function is used to encode the values of the variables to ensure that any special characters are handled correctly. The `urlencode()` function is part of PHP's core library and is always available.