我在PHP中有一个变量,我的JavaScript代码中需要它的值。如何将变量从PHP转换为JavaScript?
我有看起来像这样的代码:
<?php
...
$val = $myService->getValue(); // Makes an API and database call
?>
我有需要val
和看起来类似以下内容的JavaScript代码:
<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>