php - Check if session data isset and apply error to each item -


i have following large amount of session variables, there way can shorten code apply variables rather repeatedly writing out if(!isset($_session['whatever'])) , adding apropriate error errors array.

if(!isset($_session['fiat'])) { $errors[fiat] = 'please enter valid amount'; } if(!isset($_session['contact'])) { $errors[contact] = 'please enter valid contact'; } if(!isset($_session['name'])) { $errors[name] = 'please enter valid name'; } 

i have tried things loops , arrays struggling after serious googling, appreciated. thanks!

i have made following array unsure how use it:

$errors = array(     $_session['fiat'] => 'please enter valid amount',     $_session['contact'] => 'please enter valid contact',     $_session['name'] => 'please enter valid name',     ); 

do following? not sure goes inbetween.

for(!isset($errors)){  } 

what can following:

$errors = array();  // list session parameters , error messages want check here $valuestocheck = array(     'fiat' => 'please enter valid amount',     'contact' => 'please enter valid contact'     // , on... );  // loop through values want check , validate it. if validation doesn't pass add error message $errors array. foreach ($valuestocheck $k => $v) {      if(!isset($_session[$k])) {          $errors[$k] = $v;      } }  // check if after validation array errors not empty deal it. if (!empty($errors)) {     // errors here } 

ps: think should learn more software development basics before start writing code. useful in further career.


Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -