使用JS引号帮助PHP echo语法

Here's a snippet of my echo command:

echo 'onclick="derp(',$url,$image_dir,$file,')'

It currently outputs something like this:

onclick="derp(foobar)

But, I want it to output something like this:

onclick="derp('foobar')

Since PHP freaks out when you use quotes, I really don't know what to do.

echo 'onclick="derp(\'',$url,$image_dir,$file,'\')';
echo "onclick=\"derp(',$url,$image_dir,$file,')"

If possible use json_encode and htmlspecialchars to encode the values properly:

$onclick = "derp(".json_encode($url.$image_dir.$file).")";
echo 'onclick="'.htmlspecialchars($onclick).'"';
echo 'onclick="derp(', "'", $url, $image_dir, $file, "'", ')';