Jump to content
iwato

Abbreviated If-Else Statements

Recommended Posts

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

Share this post


Link to post
Share on other sites

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;
}

 

  • Like 1

Share this post


Link to post
Share on other sites

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

 

Share this post


Link to post
Share on other sites

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

  • Thanks 1

Share this post


Link to post
Share on other sites

Got it!  Thanks.

But, you did not answer my first question in the last entry.  Is the result not the same?

Roddy

Share this post


Link to post
Share on other sites

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.

  • Like 1

Share this post


Link to post
Share on other sites

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']), ',');
}

 

  • Thanks 1

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

I'm not concerned about the intention behind the code, I'm merely indicating which logic would be equivalent to the original code.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...