我在 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>
Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号