多个`<?php ?>`标签代表拥有不同的域吗?

多个<?php ?>标签代表拥有不同的域吗?

<form method="get">
    <input type="text" name="num1" placeholder="num1">
    <input type="text" name="num2" placeholder="num2">
    <button type="submit" name="submit" value="func1">提交</button>
</form>


<?php

global $result;
$result = 0;


if(isset($_GET['submit']) && $_GET['submit'] == 'func1'){
    $num1 = $_GET['num1'] || 0;
    $num2 = $_GET['num2'] || 0;

    $result = $num1 + $num2;

}

?>

The result is: <?php
    echo $result;
?>

比如我的这种情况,我在第二个<?php?>标签内是使用不到第一个标签内定义的$result的。

阅读 1.5k
1 个回答

同一个页面内应该是在同一个作用域内的
根据你的代码,你的QueryPrams应该是?submit=func1&num1=1&num2=3,你看一下是不是你的访问方式有问题


补充一下相关测试结果

<?php
/**
 * Created by PhpStorm.
 * User: mscststs
 * Date: 2018/8/13
 * Time: 17:02
 */



global $result;
$result = 0;


if(isset($_GET['submit']) && $_GET['submit'] == 'func1'){
    $num1 = $_GET['num1'] || 0;
    $num2 = $_GET['num2'] || 0;

    $result = $num1 + $num2;
    echo $_GET['num1'];
    echo $_GET['num2'];
    echo $num1."";
    echo $num2."";
}

?>

    The result is: <?php
    echo $result;
?>

输出是:1311 The result is: 2

clipboard.png

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题