How would I test if a cookie is set using php and if it's not set do nothing

Use isset to see if the cookie exists.

if(isset($_COOKIE['cookie'])){
    $cookie = $_COOKIE['cookie'];
}
else{
    // Cookie is not set
}

You can use array_key_exists for this purpose as follows:

$cookie = array_key_exists('cookie', $_COOKIE) ? $_COOKIE['cookie'] : null;

Depending on your needs.

// If not set, $cookie = NULL;
if (isset($_COOKIE['cookie'])) { $cookie = $_COOKIE['cookie']; }

or

// If not set, $cookie = '';
$cookie = (isset($_COOKIE['cookie'])) ? $_COOKIE['cookie'] : '';

or

// If not set, $cookie = false;
$cookie = (isset($_COOKIE['cookie'])) ? $_COOKIE['cookie'] : false;

References:

  • isset()
  • ternary operator (?)

Tags:

Php

Cookies