Jump to content

Recommended Posts

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 37

Notice: Trying to get property of non-object in C:\xampp\htdocs\core\_func.phpon line 37

Warning: Invalid argument supplied for foreach() inC:\xampp\htdocs\core\_func.php on line 38

 

please help me :D

Link to comment
https://forums.phpfreaks.com/topic/280506-need-help-with-function-for-errors/
Share on other sites

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.

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();
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.