title: How to create a simple PHP cookie language toggle?
body: <p>I'm trying to set up, what I thought would be, a simple language switch. I thought I'd use PHP cookies, but they're not behaving as intended.</p>
  
  <p>I've read a few cookie tutorials and looked at a few similar examples here on StackOverflow, but I must be missing something because it can't get it to work properly.</p>
  
  <p>I'm setting the language by passing it as a URL variable (lang=en or lang=ru). That all seems to be fine. However, the code I have at the moment that sets the cookie seems to be one step behind, so initially it has no value (I'd like it to be 'en' by default), then if the user clicks the 'ENG' button it still has no value, and then if the user clicks Russian the value shows as 'en', and then if I click the 'ENG' button again the value shows as 'ru'.</p>
  
  <p>Here's the code I've cobbled together:</p>
  
  <pre><code>if( $_GET['lang'] ) {
      $lang = (string)$_GET['lang'];
      setcookie( 'lang', $lang, time() + 60*60*24*30 );
  } elseif( !isset($_COOKIE['lang']) ) {
      $lang = 'en';
  } else {
      $lang = $_COOKIE['lang'];
  }
  </code></pre>
  
  <p>Once I've got this working I intend to use the value of the cookie to display either the English or Russian menu using a bit of conditional PHP.</p>
  
  <p>Thanks.</p>