简体   繁体   English

是否可以在 php 代码中写入 javascript function?

[英]Is it possible to write javascript function inside of php code?

I mean like我的意思是像

<?php echo "<script> javscript_function(<?php echo "$php_number_variable"; ?>); </script>" ?>

The $php_number_variable would be a number value from other php file. $php_number_variable 将是来自其他 php 文件的数字值。

Yes that's entirely possible.是的,这完全有可能。 Basically all that happens here is the value which is echoed by PHP gets embedded in the JS and the whole JS block is then passed to the browser, which runs it - effectively the same as if you'd hard-coded the value instead of echoing it.基本上这里发生的所有事情都是由 PHP 回显的值被嵌入到 JS 中,然后整个 JS 块被传递给浏览器,浏览器运行它——实际上就像你硬编码这个值而不是回显一样它。

The issue you'll have with this specific code is that you can't echo within an echo...instead just concatenate the value into the string you're already building!这个特定代码的问题是你不能在回声中回声......而只是将值连接到你已经构建的字符串中!

eg例如

<?php echo "<script> javscript_function(".$php_number_variable."); </script>"; ?>

Or, since you're using double-quote strings, the variable can be interpolated directly:或者,由于您使用的是双引号字符串,因此可以直接插入变量:

<?php echo "<script> javscript_function($php_number_variable); </script>"; ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM