如何将 var 传递给包含文件,然后使用 php、javascript 在包含文件中使用此变量


How to pass var to include file and then use this var in include file using php , javascript?

如何将var传递给包含文件(test.php),然后在包含文件(test.php)中使用此var?

首先我想在main.php中声明 var,然后我想将此 var 传递给 test.php 并在 test.php 中使用此 var(例如:echoif..else

我尝试使用php和javascript,但不能同时工作,你能给我一些建议吗?

使用 PHP

主.php

<?php
  include('test.php');
  $number = '555';
  test($number);
?>

测试.php

<?php
    function test($numeric) 
    {
       return $sample = $numeric;
    }
echo test($number);
?>

使用 JS

主.php

<?php
    include('test.php');
    $number = '555';
?>
<script type="text/javascript">
    doCallAjax();
</script>

测试.php

<script type="text/javascript">
    function doCallAjax() {
        var number = "<?PHP echo $number; ?>";
        alert(number);
    }
</script>

如果你想在多个PHP文件上共享你的数据,请考虑使用全局变量或会话,所以它会像这样:

会期:

主.php:

$_SESSION['number'] = '555';

测试.php:

echo $_SESSION['number'];

全局变量:

主.php

global $number;
$number = '555';

测试.php

global $number;
echo $number;