变量作用域在if语句中工作


variable scope work in 2 if statement

在请求anotherpost print $post后的第二个if函数中看不到任何东西,我阅读了手册并尝试
1. global $post = $_REQUEST['post']; in getpost()

2. 将第一个if的参数return $post传递给getpost($post)
两者都不工作,但它是可变范围的问题?

谢谢。

//index.php
require 'test.php';
$test = new test();
$test->getpost();

//test.php
class test{
public function getform(){
    ....
    require 'form.php';
}
public function getpost(){
    $post = $_REQUEST['post'];
    if($_REQUEST['post']){
        print $post;
        require `form_anotherpost.php`;
    }
    if($_REQUEST['anotherpost']){
        print $post;
    }
}
//form.php
<form>
    <input type="submit" name="post" value="post">
</form>
//form_anotherpost.php
<form>
     <input type="submit" name="anotherpost" value="anotherpost">
</form>

尝试将代码更改为:

public function getpost(){
    $post = $_REQUEST['post'];
    if($_REQUEST['post']){
        print $post;
    }
    if(1 == 1){
        print $post;
    }
}

这将允许您通过知道语句将返回true来查看变量是否存在于第二个if语句中。

如果我明白你在问什么,那么你应该先使用if isset,然后使用elseif。当你处理完你的数据后,你就可以打印出来了。

试试这个:

public function getpost($_REQUEST) {
    if (isset($_REQUEST['post'])) {
        print $_REQUEST['post'];
    }
    elseif (isset($_REQUEST['anotherpost'])) {
        print $_REQUEST['anotherpost'];
    }
}