iwato Posted October 6, 2019 Share Posted October 6, 2019 CHALLENGE (WELL, KIND OF): Please find below two sets of code. The first is a short-hand snippet of code fits that within a larger block of code. The second is my own long-hand interpretation of what it means. SHORT-HAND: $str_browser_language = !empty($_SERVER['HTTP_ACCEPT_LANGUAGE']) ? strtok(strip_tags($_SERVER['HTTP_ACCEPT_LANGUAGE']), ',') : ''; $str_browser_language = !empty($_GET['language']) ? $_GET['language'] : $str_browser_language; LONG-HAND: if (!empty($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { $str_browser_language = strtok(strip_tags($_SERVER['HTTP_ACCEPT_LANGUAGE']), ','); } else { if (!empty($_GET['language'])) { $str_browser_language = $_GET['language']; } else { $str_browser_language = ''; } } QUESTION: Is my interpretation correct? If not, please explain, in what manner I have erred. Roddy Link to comment Share on other sites More sharing options...
Ingolme Posted October 6, 2019 Share Posted October 6, 2019 No, that is incorrect. Each line is its own set of conditions completely independent from the other line of code. It is equivalent to the following code: if(!empty($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { $str_browser_language = strtok(strip_tags($_SERVER['HTTP_ACCEPT_LANGUAGE']), ','); } else { $str_browser_language = ''; } if(!empty($_GET['language'])) { $str_browser_language = $_GET['language']; } else { $str_browser_language = $str_browser_language; } 1 Link to comment Share on other sites More sharing options...
iwato Posted October 6, 2019 Author Share Posted October 6, 2019 OK. But, would you not agree that both longhand interpretations produce the same result? Also, how did you get to $str_browser_language = ''; and $str_browser_language = $str_browser_language; Roddy Link to comment Share on other sites More sharing options...
Ingolme Posted October 6, 2019 Share Posted October 6, 2019 You need to understand how the conditional works. In the statement X = C ? A : B , X is either equal to A or equal to B based on whether C is true or not. In other words: if(C) X=A else X=B 1 Link to comment Share on other sites More sharing options...
iwato Posted October 6, 2019 Author Share Posted October 6, 2019 Got it! Thanks. But, you did not answer my first question in the last entry. Is the result not the same? Roddy Link to comment Share on other sites More sharing options...
Ingolme Posted October 6, 2019 Share Posted October 6, 2019 No, they don't produce the same result because in your code you're skipping over some comparisons that have to be made. Regardless of whether $_SERVER['HTTP_ACCEPT_LANGUAGE'] was tested or not, you have to test $_GET['language'] in order for the statements to be equivalent. 1 Link to comment Share on other sites More sharing options...
Ingolme Posted October 6, 2019 Share Posted October 6, 2019 The shortest equivalent code you can make without conditionals would look like this: $str_browser_language = ''; if(!empty($_GET['language'])) { $str_browser_language = $_GET['language']; } else if(!empty($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { $str_browser_language = strtok(strip_tags($_SERVER['HTTP_ACCEPT_LANGUAGE']), ','); } 1 Link to comment Share on other sites More sharing options...
iwato Posted October 6, 2019 Author Share Posted October 6, 2019 Yes, I see. A visitor may wish to view the page in a language different from that of his own browser. Is this what you mean? Roddy Link to comment Share on other sites More sharing options...
Ingolme Posted October 6, 2019 Share Posted October 6, 2019 I'm not concerned about the intention behind the code, I'm merely indicating which logic would be equivalent to the original code. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now