Backslash confusion

F

freelance71

I have to pass the string '\abcd' to a function. Ofcourse one (or two) extra
'\' is needed to escape but experimenting with it shows that I have to pass
three extra backslashes to make it work.

<script>
var q = "\\\\abcd";
displayLatex(q);
</script>


/* this function is in another file */

function displayLatex(q) {


alert(q); /* this shows \abcd when called by above script*/

}


Can anyone explain why this is so? why 4 backslashes?
 
E

Erwin Moller

freelance71 schreef:
I have to pass the string '\abcd' to a function. Ofcourse one (or two) extra
'\' is needed to escape but experimenting with it shows that I have to pass
three extra backslashes to make it work.

<script>
var q = "\\\\abcd";
displayLatex(q);
</script>


/* this function is in another file */

function displayLatex(q) {


alert(q); /* this shows \abcd when called by above script*/

}


Can anyone explain why this is so? why 4 backslashes?

I do not understand.
Your script alerts \\abcd over here (on FF), as expected.
There must be more to it than you describe.

Regards,
Erwin Moller
 
F

freelance71

"Erwin Moller"
freelance71 schreef:

I do not understand.
Your script alerts \\abcd over here (on FF), as expected.
There must be more to it than you describe.

Regards,
Erwin Moller


can it be because I'm using it in a PHP file like this?

<?php

echo <<< HTMLOUT

<script>

var q = '\\\\abcd';

displayLatex(q);

</script>

HTMLOUT;

?>
 
E

Erwin Moller

freelance71 schreef:
"Erwin Moller"



can it be because I'm using it in a PHP file like this?

Yes. That is the reason.
<?php

echo <<< HTMLOUT

<script>

var q = '\\\\abcd';

that becomes:
var q = '\\abcd';

in your HTML.
Simply check your source in your browser to see it.

Regards,
Erwin Moller
 
T

Thomas 'PointedEars' Lahn

freelance71 said:
freelance71 schreef:
[...]
can it be because I'm using it in a PHP file like this?

<?php

echo <<< HTMLOUT

<script>

var q = '\\\\abcd';

displayLatex(q);

</script>

HTMLOUT;

?>

Yes, you have to escape each backslash for use with `echo'. A simple test
(with php -a) shows that

<?php

echo <<<HTML

var q = '\\a';

HTML;

?>

displays only one backslash.

The simple and most efficient solution is to take heed of this general
advice: Do not let the PHP parser do things it does not have to.

<?php
...
?>
var q = '\\a';
<?php
...
?>

(The first and last part are optional, of course.)


F'up2 cl.php

PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top