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