Q:

Issue setting cookies

I'm trying to set cookies for the first time. I've been following the documentation on W3 schools but it doesn't seem to be working for me.
Here is my code (some naming omitted for this example):
$namenospace = 'thisnameexample';

function someSetup($somename){
    $nameofcookie = $somename . '_some_value';

    if(!isset($_COOKIE[$nameofcookie])) {
        $someValue = rand(1, getrandmax());
        $cookielastsfor = time() + (86400 * 30);
        setcookie($nameofcookie, $someValue, $cookielastsfor, "/");
    }
    else {
        $someValue = $_COOKIE[$nameofcookie];
    };

    return $someValue;
};

$setSomeValue = someSetup($namenospace);

As you can see, I'm trying to check if a cookie is set, if it is, grab the value and use it, otherwise, set the cookie, then return the value anyway.
For some reason this setcookie() function isn't working. Does anyone have any insight on this?
Possibly worth noting this is a wordpress site if that's relevant?
Thanks!
Edit: have updated as per scope issues highlighted in comments and still not working as intended.
Edit again: definitely not a duplicate. My team and I discovered the issue and would like this unmarked as duplicate so an answer can be given please!

A:

Make sure you are setting your cookies before output. Where are you calling your function? 
As an alternative, you could use JS to set a cookie on client side.

