我在PHP中有一个变量,我需要在我的JavaScript代码中使用它的值。如何将我的变量从PHP变为JavaScript?
我的代码看起来像这样:
<?php
...
$val = $myService->getValue(); // makes an api and db call
?>
我有需要的JavaScript代码,val并且看起来如下:
<script>
myPlugin.start($val); // tried this, didn't work
<?php myPlugin.start($val); ?> // this didn't work either
myPlugin.start(<?=$val?> // this works sometimes, but sometimes it fails
</script>
慕娘9325324