简体   繁体   中英

outputting javascript in php using echo

can somebody tell me why this does not work:

<script>
<?
echo 'oaktree.addItem("test1", branch1, "")';
echo 'oaktree.addItem("test2", branch1, "")';
?>
</script>

When i take this out of PHP, it works like expected but when doing echo in does nothing... Thanks.

You're missing ; behind the function:

<script>
<?
echo 'oaktree.addItem("test1", branch1, "");';
echo 'oaktree.addItem("test2", branch1, "");';
?>
</script>

echo does not automatically include a new line. The above would echo as:

oaktree.addItem("test1", branch1, "");oaktree.addItem("test1", branch1, "");

Which is why the ; is mandatory for it to work. You can also include a new line yourself but adding a ; by default is a better approach.

You could have probably spotted this error by looking at the source of the page or opening up the console.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM