Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在JavaScript中使用PHP变量_Javascript_Php_Jquery_Variables_Dynamic - Fatal编程技术网

在JavaScript中使用PHP变量

在JavaScript中使用PHP变量,javascript,php,jquery,variables,dynamic,Javascript,Php,Jquery,Variables,Dynamic,几天来我一直在寻找一个有效的解决方案,但我就是无法让它发挥作用 在我的test.php <?php $testNumber = 2; ?> <script type="text/javascript" src="test.js"; $(function) { func(<?php echo json_encode($testNumber); ?>); }); </script> 当我运行test.php时,我会收到警报“函数不工作”。

几天来我一直在寻找一个有效的解决方案,但我就是无法让它发挥作用

在我的
test.php

<?php
    $testNumber = 2;
?>

<script type="text/javascript" src="test.js";
$(function) {
    func(<?php echo json_encode($testNumber); ?>);
});
</script>
当我运行
test.php
时,我会收到警报
“函数不工作”。
$testNumber
大于1


有人能帮我吗?

这是因为当您对json进行编码时,变量不再是一个数字

请尝试以下方法:

        func(<?php echo (int)$testnumber; ?>);
func();

与往常一样,您应该查看发送到浏览器的源代码-在确定某些内容不起作用的原因时,PHP的外观并不重要

<script type="text/javascript" src="test.js";
$(function) {
    func(2);
});
</script>
而不是
位于第一行,并且您不能有内部和外部的
标记

另外,
$(函数){func(2);})
甚至不是有效的JavaScript,但这并不是一个大问题,因为它从来没有被解析为JS,就像破碎的
元素上的一组奇怪的属性一样

现在,关于脚本不工作的原因,您正在调用没有参数的
func()
,因此
testNumber
未定义的
(如果您尝试
alert(testNumber)
),因此它不大于1,因此“函数不工作”。

将其更改为:

<?php
$testNumber = 2;
?>

<script type="text/javascript" >;    
    function func(testNumber) {
        if (testNumber > 1){
            alert("Function works");
        } else {
            alert("Function does NOT work");
        }
    }
    func(<?php echo $testNumber; ?>);
</script>

;    
函数func(testNumber){
如果(测试编号>1){
警报(“功能正常”);
}否则{
警报(“功能不工作”);
}
}
func();

我想您正在寻找这样的产品:

<?php
    $testNumber = 2;
?>

<script type="text/javascript">
function func(testNumber) {
    if (testNumber > 1){
        alert("function works ");
    } else {
        alert("Function1 does NOT work");
    }
}


 func(<?php echo json_encode($testNumber); ?>);
</script>

函数func(testNumber){
如果(测试编号>1){
警报(“功能正常”);
}否则{
警报(“功能1不工作”);
}
}
func();

$(文档).ready(函数(){
func();
函数func(testNumber){
如果(测试编号>1){
警报(“功能工作”);
}否则{
警报(“功能不工作”);
}
}
});

这甚至不是有效的JS。事实上,这甚至不会呈现有效的HTML。同意你的观点@h2ooooooo
json\u encode(2)
给出字符串
“2”
,但当你输出一个字符串时,它周围没有引号;),所以结果应该是
func(2)
。这部分代码是100%正确的。
<?php
    $testNumber = 2;
?>

<script type="text/javascript">
function func(testNumber) {
    if (testNumber > 1){
        alert("function works ");
    } else {
        alert("Function1 does NOT work");
    }
}


 func(<?php echo json_encode($testNumber); ?>);
</script>
<?php $testNumber = 2; ?>

<script type="text/javascript">
$(document).ready(function() {
    func(<?php echo $testNumber; ?>);

    function func(testNumber) {
        if (testNumber > 1){
            alert("function works );
        } else {
            alert("Function does NOT work");
        }
    }
});
</script>