php如何输出js语句

JavaScript010

php如何输出js语句,第1张

就像其他的输出一样即可,注意需要包含到<script type="text/javascript"></script>标签里。

例如:

echo '<script type="text/javascript">'."\n"

echo 'alert('hello')'."\n"

echo '</script>'."\n"

输出时建议最好加上 \n 换行符,这样的话,有利于源代码的阅读。

php中是不能执行js的,js只能在php解析成html后才能执行。如果想用php输出js 就要把js代码当作php的字符串来输出。

举例:

<?php

$alert  = alert(1)

echo "<script type=\"text/javascript\">".$alert."</script>"

?>

上面的代码解析成html 就会变成正常的js了。

<script type="text/javascript">alert(1)</script>

你要是想输出JSON,php里json_encode是把数组转换成JSON,json_decode是把JSON转换成数组。肯定是可以用的,你可以说一下你遇到的具体问题。

你如果只是想输出你提到的

images: ['images/sample-1.jpg', 'images/sample-2.jpg', 'images/sample-3.jpg', 'images/sample-4.jpg', 'images/sample-5.jpg', 'images/sample-6.jpg']那就拼一下字符串就好了