递增 PHP 变量在 JSCRIPT 文档工作方面


incrementing a php variable in side a jscript dosent work

<?php
$hello[1]="A";
$hello[2]="B";
$hello[3]="C";
$hello[4]="D";
$m=1;
?>
<html>
<body>
<script>
var i;
<?php $m=1; ?>
for(i=1;i<5;i++)
{
document.write("<?php echo $hello[$m]; ?> <br>");
<?php $m++; ?>
}
</script>
</body>
</html>

在上面的 PHP 代码(文件(中,它只显示"A"字母四次。 我希望它读取"hello[]"数组中的所有元素。 $m不会递增 1。 我尝试了"$m=$m+1"。 它也不起作用。 我该如何纠正?

如果你想

增加一个php变量,你应该在php中循环

<?php
$hello[1]="A";
$hello[2]="B";
$hello[3]="C";
$hello[4]="D";
?>
<html>
<body>
<script>
<?php
for($i=1;$i<5;$i++)
{
?>
    document.write("<?php echo $hello[$i]; ?> <br>");
<?php
}
?>
</script>
</body>
</html>

我和 Khan Shahrukh 一起做这件事,但看到你想做什么,我认为你真的不需要 JS 来打印这些变量。也许这更适合你:

<html>
<body>
<?php
    $hello[1]="A";
    $hello[2]="B";
    $hello[3]="C";
    $hello[4]="D";
    for($i=1;$i<5;$i++)
    {
        echo $hello[$i] . " <br>";
    }
?>
</body>
</html>

它将直接在页面中输出所需的变量,而不是使用 JS(当然,这取决于您是否真的需要它(。

这个呢

<html>
<body>
<?php
    $hello[1]="A";
    $hello[2]="B";
    $hello[3]="C";
    $hello[4]="D";
?>
<script>
<?php
    foreach($hello as $key=>$value)
    {
        echo 'document.write('.$value.');'."'n";
    }
?>
</script>
</body>
</html>

输出:

<html>
<body>
    <script>
        document.write(A);
        document.write(B);
        document.write(C);
        document.write(D);
    </script>
</body>
</html>