M
M
MaxQw2020-08-05 20:34:29
PHP
MaxQw, 2020-08-05 20:34:29

How to set COOKIE values, depending on availability?

Tell me what the function will look like.

If there is COOKIE_1, then var1 = COOKIE_1. If there is no COOKIE_1, then var1 = 0.
If there is COOKIE_2, then var1 = COOKIE_2.

Those. var1 needs to be changed depending on the presence of COOKIE.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Spartak (Web-StyleStudio), 2020-08-05
@MaxQw

Для версии php < 7
$var = isset($_COOKIE['cook_2']) ? $_COOKIE['cook_2'] : (isset($_COOKIE['cook_1']) ? $_COOKIE['cook_1'] : 0);

Для версии php >= 7
$var = $_COOKIE['cook_2'] ?? $_COOKIE['cook_1'] ?? 0;

J
John Didact, 2020-08-05
@JohnDidact

Option 1:

if(isset($_COOKIES[2])) $var = $_COOKIES[2];
elseif(isset($_COOKIES[1])) $var = $_COOKIES[1];
else $_var = 0;

Option 2:
$var = isset($_COOKIES[2]) ? $_COOKIES[2] : (isset($_COOKIES[1]) ? $_COOKIES[1] : 0);
did not check anything, but I think it should work))
And try option 3: does it work or not? Write the answer here. I'm interested in myself) $var = $_COOKIES[2] ?? ($_COOKIES[1] ?? 0);

A
Anton R., 2020-08-05
@anton_reut

if(isset($_COOKIE["test"]))
{
$var = $_COOKIE["test"];
}
else 
{
$var = 0;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question