Select to substitute a value from cookie?

Good day! Implement on the website (Bitrix) selector currency.
Doing so
<form name="form" action="" method="get">
 <select class="form-control" name="currency" id="currency" onchange="this.form.submit()">
 <option value="RUB" >RUB</option>
 <option value="USD" >USD</option>
<? setcookie('BX_CURRENCY', $_GET['currency'], strtotime( '+30 days' ), SITE_DIR);?>
<? echo $_COOKIE["BX_CURRENCY"]; ?>

Maybe not an elegant way, I will be glad to criticism with examples.
In this example successfully write to cookie data, but of course when you reload the page the cookie is able by default.

Lost in broad daylight, it is necessary to substitute the selected value from $_COOKIE["BX_CURRENCY"];
April 7th 20 at 11:04
2 answers
April 7th 20 at 11:06
1. output the cookie immediately after installation doesn't make sense – it will be available only after a page reload.
2. just check cookie exists or not before installing it.
3. to stop using short tags.
echo is made the test just...
A concrete example can help? - anahi9 commented on April 7th 20 at 11:09
@anahi9, about
if(! isset($_COOKIE["BX_CURRENCY"])) {
 setcookie('BX_CURRENCY', $_GET['currency'], strtotime( '+30 days' ), SITE_DIR);
- Carmine_Bo commented on April 7th 20 at 11:12
@Carmine_Bo, Thank You! Yes, it is a check for empty, I realized.
But how to drive is needed in the choice selectra ?

<option value="USD" <?if($_COOKIE['BX_CURRENCY']==RUB):?>selected="selected"<?endif?>>RUB</option>

Or each time you select in the selector to do setcookie ? - anahi9 commented on April 7th 20 at 11:15
@anahi9, every time. people could easily want to change the currency. - Carmine_Bo commented on April 7th 20 at 11:18
@Carmine_BoHow to portray it in select then? that does not work =( - anahi9 commented on April 7th 20 at 11:21
<option value="USD" <?= ($_COOKIE['BX_CURRENCY']== 'RUB') ? 'selected="selected"' : "?>>RUB</option>
- Carmine_Bo commented on April 7th 20 at 11:24
April 7th 20 at 11:08
So I decided
$select = array(1=>",2=>");

if(isset($_POST['select'])) {
 $select[(int)$_POST['select']] = 'selected'; 
} elseif(isset($_COOKIE["select"])) { 
 $select[(int)$_COOKIE["select"]] = 'selected';
<form method="post">
<select name="select" onchange="this.form.submit()">
<option value="1" <?=$select[1]?>>1</option>
<option value="2" <?=$select[2]?>>2</option>

Find more questions by tags PHP