If you're passing the variable in a hidden (or even non-hidden) form field, do what laserlight said.
If you're inserting the variable directly into a URL (for <a href="myscript.php?foo=bar"> or similar), look at the information and examples in the [man]urlencode/man manual page. Example 2 is what you want (which uses both urlencode() and htmlentities()).