-7

I have an problem with my cookie section. I want to save and get my cookies but if the cookie isn't set i get and 'underfind index'. So i tried isset() around the cookie but i can't get the value only an bool.

Code:

['gender' => isset($_COOKIE['age']),]

result:

["gender"]=> bool(false)

What i need:

["gender"] => string(1) '1'

var_dump $_COOKIE

["gender"]=> string(1) "1"

Why give isset() an bool as result and not the real value as result.

Cookie save: Cookies Chrome overview

The problem is, if cookie is set i'll get an value but if the cookie is set and i've the function isset() arround the cookie it'll show an bool and not the real value.

  • 3
    check the docs: http://php.net/manual/en/function.isset.php isset returns a bool – Edwin Jul 19 '17 at 09:37
  • `isset()` appears to return if the cookie is set, either yes or no, at which point you can then get the value of the cookie if yes – Nick is tired Jul 19 '17 at 09:38
  • The cookie is set, because if i doesn't use isset() it works. but i don't want the error. underfind index when it isn't set. – Pieter Dijkstra Jul 19 '17 at 09:39
  • 1
    Possible duplicate of [PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"](https://stackoverflow.com/questions/4261133/php-notice-undefined-variable-notice-undefined-index-and-notice-undef) – gp_sflover Jul 19 '17 at 09:42
  • The answer on my question above is wrong, because this question is about display bool on isset on a var that is alrady set. – Pieter Dijkstra Jul 19 '17 at 09:45

2 Answers2

4

Why give isset() an bool as result and not the real value as result.

Because isset() returns a bool. You need to return your cookie value if isset() is eval'd to true :

['age' => (isset($_COOKIE['age']) ? $_COOKIE['age'] : false),]

The above line will return the value if the cookie is set, and false otherwise.

roberto06
  • 3,844
  • 1
  • 18
  • 29
0

isset always gives you a Boolean return. kindly echo the result of your requested cookie like this:

if(isset($_COOKIE['age']){
{
   $_COOKIE['age'];
}
Bug Inspector
  • 301
  • 4
  • 15