`未定义索引`错误抱怨foreach迭代数组中的变量值

I am trying to write a func to replace a large chunk of repetitive code:

$inputVals = array( $siteId, $siteName, $numHspaBbu, $numLteBbu, $extAlarmsTerminated, $cellLowPower );

    function updateVarVals( $inputVals ) { // assign values to $vars after Update button clicked      
      foreach( $inputVals as $val ) {
        if ( $_POST[ $val ] !== "" ) { // if $_POST value is not empty then assign to $var and corresponding $_SESSION value  
        $val = $_SESSION[ $val ] = $_POST[ $val ];
        } else { // re-assign $_SESSION value to $var
          $val = $_SESSION[ $val ];
        } // close IF
      } // close FOREACH
    } // close FUNC

    updateVarVals( $inputVals );

but no matter how I quote $val I keep getting:

Notice: Undefined index: <$val value> in C:\xampp\htdocs\database on line 145.

for every iteration of the loop. Why is PHP expecting variable values to be defined here?

Well if Your $_POST[$val] does not exist, it'll throw that error.

If you replace if ( $_POST[ $val ] !== "" ) { with if ( empty($_POST[ $val ]) ) {, which checks if a variable is set and not empty, then you shouldn't be getting that error anymore.