Mlaaa Posted July 25, 2013 Share Posted July 25, 2013 Hi all I have problem with making custom function to deal with my scripts i tried and look on google but cant find here is code i have function errors() { $errors = array(); $_SESSION['errors'] = $key->$errors; foreach ($_SESSION['errors'] as $error) { return $error; } } and i use it like this in my script $error[] = 'Please enter username and password.'; but when i call function i have errors Notice: Undefined variable: key in C:\xampp\htdocs\core\_func.php on line 37Notice: Trying to get property of non-object in C:\xampp\htdocs\core\_func.phpon line 37Warning: Invalid argument supplied for foreach() inC:\xampp\htdocs\core\_func.php on line 38 please help me Quote Link to comment https://forums.phpfreaks.com/topic/280506-need-help-with-function-for-errors/ Share on other sites More sharing options...
mac_gyver Posted July 25, 2013 Share Posted July 25, 2013 what exactly are you trying to get your function to do? is it supposed to format and output the errors? add an error to the $error array? do something with a $_SESSION variable? setting $error[] = 'Please enter username and password.'; is about as simple as this can get. what do you need a function for? to write a function, you must first define what inputs (if any) it uses, what processing it will do, and what result or output it produces. Quote Link to comment https://forums.phpfreaks.com/topic/280506-need-help-with-function-for-errors/#findComment-1442140 Share on other sites More sharing options...
Mlaaa Posted July 25, 2013 Author Share Posted July 25, 2013 i want to store errors in session array and then display it on html page with just calling function errors() Quote Link to comment https://forums.phpfreaks.com/topic/280506-need-help-with-function-for-errors/#findComment-1442142 Share on other sites More sharing options...
KevinM1 Posted July 25, 2013 Share Posted July 25, 2013 There's... a lot wrong with what you currently have, and it really highlights a lack of understanding about custom functions, arrays, and the difference between return and echo. $_SESSION is already an array. If you want an $errors array to be stored in $_SESSION, then you simply assign each error to the array, then assign that to $_SESSION, like so: session_start(); // IMPORTANT! You need this at the top of every file you want to access sessions in $errors = array(); // somewhere else in your code where you want to store an error $errors[] = "ERROR: You can't do what you tried to do!"; // note the []... that tells it to assign the value on the right to the // next open spot in the array on the left // even lower in your code $errors[] = "ERROR: You still can't do that!"; // assign it to $_SESSION $_SESSION['errors'] = $errors;So, on your display page, you can have: session_start(); function errors() { if (isset($_SESSION['errors'])) { // code defensively to prevent errors foreach ($_SESSION['errors'] as $error) { echo "$error<br />"; } } } // blah blah more code errors(); Quote Link to comment https://forums.phpfreaks.com/topic/280506-need-help-with-function-for-errors/#findComment-1442162 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.