short.fuse.5254
short.fuse.5254

Reputation: 13

How do I rewrite this conditional as one conditional instead of two nested ones?

I'm trying to get through a book & I'm on chapter two so I figure this part can't be that hard, but I can't figure it out.

It wants me to re-write this part as one conditional instead of two nested ones -It also gives a hint saying that I'll have to use the AND operator-:

if (isset($_REQUEST['gender'])) {
  $gender = $_REQUEST['gender'];

  if ($gender == 'M') {
    echo '<p><b>Good day, Sir!</b></p>';
  } elseif ($gender == 'F') {
  echo '<p><b>Good day, Madam!</b></p>';
  } else { // Unacceptable Value.
    $gender = NULL;
    echo '<p class="error">Gender should be either "M" or "F"!</p>';
  }

} else { // $_REQUEST['gender'] is not set.
  $gender = NULL;    
  echo '<p class="error">You forgot to select your gender!</p>';  
}

This was the solution I used:

if (isset($_REQUEST['gender']) && $_REQUEST['gender']=='M'){
$gender = $_REQUEST['gender'];
echo '<p><b>Good day, Sir!</b></p>';
}
elseif (isset($_REQUEST['gender']) && $_REQUEST['gender']=='F'){
$gender = $_REQUEST['gender'];
echo '<p><b>Good day, Madam!</b></p>';
}
else{
$gender = NULL;
echo '<p class="error">You forgot to select your gender!</p>';  
}

Upvotes: 0

Views: 122

Answers (4)

raidenace
raidenace

Reputation: 12836

Just for the heck of it! (As in, this will work, but is pretty cryptic :-)

echo (array_key_exists("gender",$_REQUEST))?
(strtoupper($_REQUEST['gender'])=="M")?"Good day, Sir!":
((strtoupper($_REQUEST['gender'])=="F")?"Good day, Madam!":
"Gender should be either 'M' or 'F'!"):"You forgot to select your gender!";

As a single line...

echo (array_key_exists("gender",$_REQUEST))?(strtoupper($_REQUEST['gender'])=="M")?"Good day, Sir!":((strtoupper($_REQUEST['gender'])=="F")?"Good day, Madam!":"Gender should be either 'M' or 'F'!"):"You forgot to select your gender!";

Look Ma, no ifs! ;-)

Upvotes: 1

David Wilkins
David Wilkins

Reputation: 584

Using a switch statement, you only need one if

if (!isset($gender))
  echo "no gender specified";
else
  switch($gender) {
    case "M":
    case "m":
      echo "Good day, Sir"; break;
    case "F":
    case "f":
      echo "Good day, Madam"; break;
    default:
      echo "What gender are you?";
  }

Upvotes: 0

RNK
RNK

Reputation: 5792

This will check your condition without nested loop. Use AND operator.

if (isset($_REQUEST['gender']) && $_REQUEST['gender']=='M'){
    echo '<p><b>Good day, Sir!</b></p>';
}
else if (isset($_REQUEST['gender']) && $_REQUEST['gender']=='F'){
    echo '<p><b>Good day, Madam!</b></p>';
}
else if(isset($_REQUEST['gender']) && ( $_REQUEST['gender']!='M'
                                        || $_REQUEST['gender']!='F')){
    echo "INVALID INPUT";
}
else{
  echo '<p class="error">You forgot to select your gender!</p>';  
}

Upvotes: 1

Aleksei Matiushkin
Aleksei Matiushkin

Reputation: 121010

if (!isset($_REQUEST['gender'])) {
  echo "BAH";
} elseif (strtoupper($_REQUEST['gender']) === 'M') {
  echo "SIR";
} elseif (strtoupper($_REQUEST['gender']) === 'F') {
  echo "MADAM";
} else {
  echo "UNKNOWN";
}

Upvotes: 3

Related Questions