if 语句 (PHP) 的变量范围问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4110340/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 12:00:05  来源:igfitidea点击:

Variable Scope issue with if statements ( PHP)

phpif-statementscope

提问by Andre

Alright I seem to have a misconception with variable scope with PHP, forgive my lack of the subject as I come from a Java, C# background. Thinking I could make variables accessible to functions or if statements simply by placing it outside it. Below is a snippet of what I'm trying to accomplish:

好吧,我似乎对 PHP 的变量范围有误解,请原谅我缺乏该主题,因为我来自 Java、C# 背景。认为我可以简单地将变量放在函数或 if 语句之外,从而使变量可以访问它。下面是我试图完成的一个片段:

foreach ($nm as $row=>$im) {
    $itm_name = $im;
    $lnk = $lnk_cty[$row];  
    if($mode == 'addMenu') {
        $m = $m_id; //id will be coming from fresh insert of menu_name 
    } else {
        $m = $_POST['mnu_add'][$row];
        echo "MENU_ID: ".$m;
    }
    if($mode == 'addCat') {
        $m = $c_id; //id will be coming from fresh insert of cat_name
    } else {
 $m = $_POST['cat_add'][$row];
    }
    //used for testing purposes
    echo "item name: ".$itm_name ."<br />";
    echo "lnk: ".$lnk ."<br />";
    echo "m: ".$m ."<br />"; //$m is empty here, because its a new declaration as oppose to accessing $m value from if statement
    $display_fields .= "<li>".$itm_name." ".$item."</li>";
    $sql_array[] = '("' . $itm_name . '", "' . $lnk . '",  ' . $m . ')';  // Add a new entry to the queue 
}

Now what I'm trying to do is make the $mvariable values accessible outside the if statements its in to the $mvariable used in the $sql_array[]statement. In C# I would simply declare a variable outside the foreach loop and be able to use it. After doing some reading on the matter I found that using the global or GLOBALS keywords would only work if my global scope variable is assign the value before the foreach, and declaring global $mto obtain that value within the loop. But with my current code $mis of a local scope within the if statements and everyone discourages using them. Now, is there a better method of making $maccessible to the $sql_array[]statement?

现在我要做的是使$m变量值在 if 语句之外可以访问到语句中使用的$m变量中$sql_array[]。在 C# 中,我只需在 foreach 循环之外声明一个变量并能够使用它。在对此事进行了一些阅读后,我发现使用 global 或 GLOBALS 关键字仅在我的全局范围变量在 foreach 之前分配值并声明global $m在循环中获取该值时才有效。但是我当前的代码$m是 if 语句中的局部范围,每个人都不鼓励使用它们。现在,有没有更好的方法$m可以访问该$sql_array[]语句?

回答by Stephen

Ifstatement blocks do not have their own scope. Whatever data you are assigning to $mmust be empty to begin with. Try debugging things like your $_POSTvariables. Also, where is $m_idbeing defined? Maybe it is empty as well.

If语句块没有自己的作用域。无论您分配给什么数据$m,一开始都必须是空的。尝试调试诸如$_POST变量之类的东西。另外,在哪里$m_id定义?也许它也是空的。

PHP doeshave scope inside functions, class methods and the like. But ifstatements do not have their own scope. For example, the following code would echo Hi there!:

PHP在函数、类方法等内部确实有作用域。但是if语句没有自己的作用域。例如,以下代码将 echo Hi there!

$bool = true;
if ($bool) {
    $new_var = 'Hi there!';
}
echo $new_var;

Have a read in the manual.

阅读手册

回答by Andre

Its true that if statements does NOT have scope , but It seems that there is a problem with the Scope (piece of code in braces)..in the below code snippet the expected result is : $z[0] = 0, $z[1] = 1 but the Actual output obtained is $z[0] = 100; and $z[1] is undefined. The PHP used is from Apache Friends distribution

确实,如果语句没有作用域,但似乎作用域有问题(大括号中的一段代码)。在下面的代码片段中,预期结果是:$z[0] = 0, $z [1] = 1 但得到的实际输出是 $z[0] = 100; 并且 $z[1] 是未定义的。使用的 PHP 来自 Apache Friends 发行版

<?php

$x = 0;
$y = 1;
$z[0]=100;

if($x!=0){
    $z[0]=1;
    $z[1]=2;
    $z[2]=3;
    $z[3]=4;
}else{
    if($y == 0){
        $a =1;
    }else{
        global $z;
        echo $z[0];
        echo $z[1];
    }
}

?>