I\'m looking for the best way to escape some Javascript text in PHP, and json_encode is the wrong tool for the job.
json_encode
The problem comes from this line:
json_encode is the right tool for the job. Your problem arises from the fact that you are also including that Javascript in an HTML attribute, thus it also needs to be htmlspecialchars-encoded.
htmlspecialchars
echo " onclick=\"SwitchDiv(" . htmlspecialchars(json_encode($option)) . ")\"";