21

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?

3
  • what does it mean: 'id will be coming?' Commented Nov 5, 2010 at 22:01
  • The source of the ID would be ... Commented Nov 5, 2010 at 22:04
  • @Aurel300 thank you mr. Translator. Could you please tell the actual $m_id value in the runtime? Commented Nov 5, 2010 at 22:10

1 Answer 1

77

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.

2
  • @Stephen, $m_id is coming from a resultset from a mysql table. When testing it with the echo statements in the code it shows the value for $m when assigned that id and from the resultset and also when assigning the $_POST variable to it in the else block. Here is a snippet: $results = statement("select menu_id from $menu where menu_name = '$menu_nm'"); while ($recordset = mysql_fetch_array($results)) { if($mode == 'addCat'){ $c_id = $recordset['cat_id']; }else{ $m_id = $recordset['menu_id']; } }
    – Andre
    Commented Nov 5, 2010 at 22:40
  • 1
    One of these is empty: $m_id, $_POST['mnu_add'][$row], $c_id, or $_POST['cat_add'][$row]
    – Stephen
    Commented Nov 5, 2010 at 22:44

Not the answer you're looking for? Browse other questions tagged or ask your own question.