最佳答案
我在PHP中有一个变量,我需要它的值在我的JavaScript代码中。如何将我的变量从PHP获取到JavaScript?
我有如下代码:
<?php
$val = $myService->getValue(); // Makes an API and database call
在同一页面上,我有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>