For the URI query value use urlencode/urldecode; for anything else use rawurlencode/rawurldecode.
To create entire query string use http_build_query()
The difference between urlencode and rawurlencode is that
urlencodeencodes according to application/x-www-form-urlencoded (space is encoded with+) whilerawurlencodeencodes according to the plain Percent-Encoding (space is encoded with%20).
For the URI query value use urlencode/urldecode; for anything else use rawurlencode/rawurldecode.
To create entire query string use http_build_query()
The difference between urlencode and rawurlencode is that
urlencodeencodes according to application/x-www-form-urlencoded (space is encoded with+) whilerawurlencodeencodes according to the plain Percent-Encoding (space is encoded with%20).
The cunningly-named urlencode() and urldecode().
However, you shouldn't need to use urldecode() on variables that appear in $_POST and $_GET.
You can use the function html_entity_decode() and the HTML entitycode for ยฒ which is ².
Your code will look something like this:
<meta charset="utf-8">
<script>
document.write(encodeURIComponent(';<?php echo html_entity_decode("²") ?>'));
</script>
You can use the urlencode() function:
echo urlencode(';ยฒ');
The above function outputs %3B%C2%B2.