Which one to use?
Use rawurlencode() for URL path segments and query values — it follows RFC 3986 and encodes spaces as %20. Use urlencode() only for form data submitted as POST bodies where spaces as + is expected.
Building query strings
In PHP, use http_build_query($array) to build query strings correctly — it handles encoding automatically. Use parse_str($query, $output) to parse them back.
Double encoding
A common bug is encoding a value that's already encoded. The result has %25 instead of % in the output. Always decode before re-encoding if you're not sure of the current state.
URL vs HTML encoding
URL encoding and HTML encoding are different things. Always use htmlspecialchars() when outputting user-supplied data in HTML, even if it came from a URL parameter.
URL handling, query strings, redirects, and HTTP — covered in our PHP developer ebooks and courses.