Jump to content

[SOLVED] 'if ... else if ... else' conditional statement


xProteuSx

Recommended Posts

Oh man ... I think I don't have enough caffeine in me.  I am trying to do something extremely simple, yet I can't get it to work.  Here is the basic idea.

 

-------------------------------------------

$genderstatusrow = 'F';

echo $genderstatusrow;

if ($genderstatusrow = 'M')

{

$genderstatus = 'Male';

}

else if ($row[7] = 'F')

{

$genderstatusrow = 'Female';

}

else

{

$genderstatusrow = 'Not Specified';

}

echo $genderstatusnow;

-------------------------------------------

 

This code, when run, displays:

 

FM

 

WTF??

Link to comment
Share on other sites

SORRY.  Below is the correct code and output ...

 

-------------------------------------------

$genderstatusrow = 'F';

echo $genderstatusrow;

if ($genderstatusrow = 'M')

  {

  $genderstatus = 'Male';

  }

else if ($row[7] = 'F')

  {

  $genderstatusrow = 'Female';

  }

else

  {

  $genderstatusrow = 'Not Specified';

  } 

echo $genderstatus;

-------------------------------------------

 

This code, when run, displays:

 

FMale

 

WTF??

Link to comment
Share on other sites

I think that I have exhausted every combination of '=' and '==' and still no cigar.  If I do as you say, and use the following code I still get a foobar result:

 

$genderstatusrow = 'F';

echo $genderstatusrow;

if ($genderstatusrow == 'M')

        {

$genderstatus = 'Male';

}

else if ($row[7] == 'F')

{

$genderstatusrow = 'Female';

}

else

{

$genderstatusrow == 'NotSpecified';

}

echo $genderstatus;

 

This will get you the following output:

 

F

Link to comment
Share on other sites

Glad you got it sorted = )

 

Let's format code so it's more readable, hence easier to troubleshoot:

 

<?php
$genderstatusrow = 'F';
echo $genderstatusrow;
if ($genderstatusrow == 'M') {
    $genderstatus = 'Male';
} elseif ($row[7] == 'F') {
    $genderstatusrow = 'Female';
} else {
    $genderstatusrow == 'NotSpecified';
}
echo $genderstatus;
?>

 

I can see how you got the result 'F'. You told it $genderstatusrow was 'F' to begin with, then you asked it to echo that, hence 'F' goes to screen. If anything but 'M' resulted in TRUE, then $genderstatusrow changes. You then asked to echo $genderstatus. Since $genderstatus doesn't exist, nothing prints, leaving only the initial 'F' echo'd to begin with. If 'M' resulted in TRUE, then you would have gotten 'FMale' printed, due to the first 'F', then 'Male' for $statusgender, and being that there is no HTML break after any of the echos, they print side-by-side.

 

Ok, so you fixed your logic and variables... let's expand on the = vs == stuff...

 

= is an assignment, not an operator. == is an operator (comparison operator), and does not assign anything. It only evaluates. So:

<?php
$a = 'this'; // assigns the string 'this' to $a, and evaluates $a as TRUE
unset ($a) // clear the variable
$a == 'this'; // assigns nothing to $a (it remains cleared), and evaluates $a to FALSE

 

If a variable has a value assigned to it, it is always TRUE. Let's look at that inside some code:

 

<?php
$a = 'I am here!';
if ( $a == TRUE ) {
    echo $a;
}
?>

Outputs 'I am here!' to screen

 

So let's see... if assigning a value to $a makes it TRUE, and PHP's IF control structure branches on TRUE or FALSE, then:

 

<?php
unset ($a) // $a has no value, it doesn't exist
if ( $a = 'I am here!' ) {
    echo $a;
}
?>

 

Outputs 'I am here!' to screen again, even though $a had no value prior to the IF evaluation! Essentially, we have combined two things together into one statement, an assignment and an evaluation. This is a shortened version of this code directly above it. They perform and evaluate exactly the same way.

 

Speaking of shorthand, we do not have to write out == TRUE. It is assumed that's what we want to know:

 

<?php
unset ($a); // clear
$a = 'I am here!'; // assignment, it is now TRUE (it exists with a value)
if ( $a ) {
    echo $a;
}
?>

Prints 'I am here!' to screen once again!

 

Some coders frown on this type of shorthand, but it is valid code nonetheless.

 

Let's see how that works with FALSE (long and short example):

 

<?php
unset($a);
if ( $a == FALSE ) {
    echo '$a died one statement ago. Sorry for your loss!';
}
?>

Prints '$a died one statement ago. Sorry for your loss!'

<?php
unset($a);
if ( !$a ) {
    echo '$a died one statement ago. Sorry for your loss!';
}
?>

Also prints '$a died one statement ago. Sorry for your loss!'

 

So anyways, when comparing, use == (and I assume you know the difference between == and === ?)

 

To close, I will re-post your original code (using =) and comment on what was going on.

 

<?php
$genderstatusrow = 'F';
echo $genderstatusrow; // Prints 'F' to screen.
if ($genderstatusrow = 'M') {
    $genderstatus = 'Male';
    // this will ALWAYS evaluate TRUE and execute.
    // variable status:
    // $genderstatusrow = 'F'
    // $genderstatus = 'Male'
} elseif ($row[7] = 'F') {
    $genderstatusrow = 'Female';
    // this WOULD evaluate as TRUE, but since the initial IF will
    // always be TRUE, this portion will never execute.
    // $genderstatusrow can NEVER equal 'Female'
    // variable status: unchanged
} else {
    $genderstatusrow = 'NotSpecified';
    // and this would execute if the IF -and- the ELSEIF both evaluated
    // as FALSE, but since the initial IF will always be TRUE, this will
    // never execute.
    // variable status: unchanged
}
echo $genderstatus; // Prints 'Male'.
?>

Result:

'FMale' printed to screen

 

PhREEEk

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.