I want to make a function that change the value of global variable dynamically with a loop. i tried to to put 'global' behind the $$key but it throws error. is it possible to do that?
define('INPUT_FIELDS', array('fullname', 'phone', 'email')); // the input fields names that every form must have
$fullname = $phone = $email = "";
function isPostValid(){
if ( $_SERVER['REQUEST_METHOD'] === 'POST' ){
foreach ($_POST as $key => $value) {
if( in_array($key, INPUT_FIELDS) ){// only if the key is exists in the inputs we chose
$$key = checkInputsAndValidate($key);
if(!$$key){// if the variable is null then break the loop and make change the variable to false
return false;
break;
}
}
}//END foreach
return true;
}
return false;
}
&$keysyntax for referenced variables? If not then you would need to useglobal $key;as you enter your function to "expose" it.