Is it possible to set a cookie with PHP and read it with JavaScript to overcome cookie availability timing issues?

When dealing with cookie availability timing issues between PHP and JavaScript, one possible solution is to set a cookie with PHP and then read it with JavaScript. By doing this, you can ensure that the cookie is available for both server-side and client-side scripts at the desired time.

<?php
// Set a cookie with PHP
setcookie("example_cookie", "example_value", time() + 3600, "/");

// Output JavaScript to read the cookie
echo '<script>';
echo 'var cookieValue = document.cookie.split("; ").find(row => row.startsWith("example_cookie=")).split("=")[1];';
echo 'console.log("Cookie value retrieved with JavaScript: " + cookieValue);';
echo '</script>';
?>