0

i'm getting this even using 'isset':

Notice: Undefined index

it's giving the error at:

returnifisset($_COOKIE["miceusername"], ' value="', '"');

even though i am checking if the cookie isset or not. The function is:

function returnifisset($variable, $first = '', $last = ''){
    if(isset($variable) and !empty($variable)){ return $first.$variable.$last; }
}

how i should modify this function to make it work and not give that error!

5
  • 7
    You are not using isset until you are inside the function. PHP looks at the cookie and the index before the variable is processed by the function. That is where your error is coming from. Commented Aug 17, 2011 at 17:27
  • 1
    You would need to do printifisset(isset($COOKIE["miceusername"], ' value="', '"');) which totally defeats the purpose of using the function at all. My suggestion is to skip the function altogether as it is rather unnecessary and the savings you get from typing it in full are minimal if any at all. Commented Aug 17, 2011 at 17:36
  • @Ameer Replace the function with code. Instead of calling returnifisset substitute the code within the function. Also, just to make sure you know: Calling return from returnifisset would not return a value for the calling context. You would have to use return returnifsset('...','...','...'); if you wanted that. Commented Aug 17, 2011 at 17:38
  • @Jrod it works that way but why? why this way? why not with a function? Commented Aug 17, 2011 at 17:45
  • It's because php looks at the variable you are passing to the function before executing the function itself. Commented Aug 17, 2011 at 18:40

2 Answers 2

1

You are actually accessing the variable by passing it with your function, before the isset is ever called. You can't solve this problem.

Sign up to request clarification or add additional context in comments.

Comments

0

You use different function names printifisset and returnifisset. Also you can use only !empty() statement

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.