Jump to content

Abbreviated If-Else Statements


iwato

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

Link to comment
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
Link to comment
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

 

Link to comment
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
Link to comment
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

Link to comment
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
Link to comment
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
Link to comment
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

Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...