I have JS with an options var like so :
$output .=
'<script type="text/javascript">
// <![CDATA[
var options = {
render: "canvas",
size: "100",
radius: "0.5",
};
// ]]>
</script>';
But I need to output it ( echo ) with PHP variables and it can not be in a separated file, So I did the output using normal concatenation with a point . e.g. '.$var.'but since my JS needs double quotes " I had put them also into the mix resulting in double-single quote sequence .
var options = {
render: "'.$q_render.'", // Canvas, Div ,Image
size: "'.$q_size.'",
radius: "'. $q_corner_r.'",
};
And it works as far as my tests goes .
The plot thickens , when I need the specific value of $q_corner_r to be multiplied by a factor :
var options = {
render: "'.$q_ender.'", // Canvas, Div ,Image
size: "'.$q_size.'",
radius: "'. ($q_corner_r )* 3 .'",
};
Which also works . But the real problem is that I needed to be a decimal value and thus multiple by a decimal factor .
var options = {
render: "'.$q_ender.'", // Canvas, Div ,Image
size: "'.$q_size.'",
radius: "'. ($q_corner_r )* 0.3 .'",
};
At this point , PHP throws an error , because for all it knows, the decimal point in 0.3 is actually an end concatenation point.
my layman solution to the problem was to wrap the problem in json_encode() and send it to battle naked and defenseless.
radius: "'. json_encode( ($q_corner_r )* 0.1 ) .'",
which , very surprisingly, works ok.
So everything works now ( don´t panic ) - but my doubt remains.
How should I handle this situation ? ( I know I CAN do $qr_corner_r = ($qr_corner_r )* 0.1 in PHP before the JS .. but SHOULD I ? )
It is only for pure luck ( or stupidity, or a combination of both ) that my code works .
Is there any other solution / method adequate for these situations ?
Are we merely observing a simply bad "wrong escaping" case ? ( or no-escaping-at-all in my case )