Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variable Scope issue with if statements (PHP)

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:

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 $m variable values accessible outside the if statements its in to the $m variable 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 $m to obtain that value within the loop. But with my current code $m is of a local scope within the if statements and everyone discourages using them. Now, is there a better method of making $m accessible to the $sql_array[] statement?

like image 582
Andre Avatar asked Nov 05 '10 21:11

Andre


People also ask

Can you declare a variable in an if statement PHP?

If you're new to the syntax that's used in the code sample, if (int i = 5) { is a perfectly valid way of declaring and defining a variable, then using it inside the given if statement. It allows us to write terser, clearer code, while also avoiding limiting the scope of a variable.

How do you use a variable outside of an if statement in PHP?

Right now $uname is only in scope within the if statement, once you leave the if statement the variable no longer exists. PHP has no block scope for variables, so they are available until the end of the function once they have been assigned a value.

What is the default scope of IF statement?

if is a conditional statement, which returns true or false based on the condition that is passed in it's expression. By default, if-statement is implemented on only one line, that follows it. But if we put block after the if-statement, it will be implemented on the whole block.


1 Answers

If statement blocks do not have their own scope. Whatever data you are assigning to $m must be empty to begin with. Try debugging things like your $_POST variables. Also, where is $m_id being defined? Maybe it is empty as well.

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

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

Have a read in the manual.

like image 68
Stephen Avatar answered Sep 27 '22 21:09

Stephen