javascript - Writing a variable into a cookie and subsequent reading in PHP

one text

Trying to store a variable into a cookie in JS and while trying to read the variable in the same run I get the previous value of the variable stored earlier.

<script>
...
setCookie('myVariable','1234',expiration);
</script>
<?php

print_r($_COOKIE['myVariable']);

?>

For example if in myVariable i have stored value '4321' previously, in the example code I don't see '1234' until the next refresh of the page but the old value of '4321'.

When is the cookie variable actually updated?

Source