xProteuSx Posted November 24, 2007 Share Posted November 24, 2007 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?? Quote Link to comment https://forums.phpfreaks.com/topic/78657-solved-if-else-if-else-conditional-statement/ Share on other sites More sharing options...
xProteuSx Posted November 24, 2007 Author Share Posted November 24, 2007 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?? Quote Link to comment https://forums.phpfreaks.com/topic/78657-solved-if-else-if-else-conditional-statement/#findComment-398008 Share on other sites More sharing options...
PHP_PhREEEk Posted November 24, 2007 Share Posted November 24, 2007 When you say: if ($genderstatusrow = 'M') You are assigning a value to a variable. IF returns TRUE because it successfully populated the variable for you. You are trying to compare, so you need == if ($genderstatusrow == 'M') Fix any other instances. PhREEEk Quote Link to comment https://forums.phpfreaks.com/topic/78657-solved-if-else-if-else-conditional-statement/#findComment-398009 Share on other sites More sharing options...
xProteuSx Posted November 24, 2007 Author Share Posted November 24, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/78657-solved-if-else-if-else-conditional-statement/#findComment-398010 Share on other sites More sharing options...
xProteuSx Posted November 24, 2007 Author Share Posted November 24, 2007 I caught onto a stupid mistake ... what a surprise. All is well. Thanks for the help. Good thing I got another shot of caffeine into me! :-\ Quote Link to comment https://forums.phpfreaks.com/topic/78657-solved-if-else-if-else-conditional-statement/#findComment-398012 Share on other sites More sharing options...
PHP_PhREEEk Posted November 24, 2007 Share Posted November 24, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/78657-solved-if-else-if-else-conditional-statement/#findComment-398078 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.