The url encoding is required when you are trying to programatically assign parameters onto an URL for query, page redirection and so on.
So, what's the different between the urlencode
From the output point of view, both urlencode
returns the same result except
- urlencode encode space character into + (plus), but rawurlencode encode the space character into %20.
- urlencode encode ~ into %7E, but rawurlencode didn't percent-encode this character.
Both the urlencode and rawurlencode will percent-encode the reserve characters by replacing them with % followed by 2 hex digits.
Below please find the table of reserved and unreserved characters:
Note: The ~(tildes) will be percent-encoded by urlencode. Please see the above description.
Below please find the value of percent-encoded for the reserved characters:
When should I use the urlencode or rawurlencode? It's depends on the usage, but I will recommend the use of
rawurlencode for the url encoding with no query string, and urlencode to encode particularly for the query string (after the ?).
It is due to the percent-encoded of application/x-www-form-urlencoded media type for urlencode.
Try it yourself: