php if argument

garrensilverwing

New Member
Messages
148
Reaction score
0
Points
0
hey again guys,

i am having a new problem. i am still working on creating a member profile feature for my website but i am having a problem with an if argument. when the person goes to the edit profile page i want all the information that is already in the profile to show up and be edited. i have the gender options set in a dropdown box and i want the current gender to be already selected on the edit profile page. so i figured an easy way would be a simple if statement saying if $'gender = M then the selected="selected" would be echoed for that options in the drop down box. the problem is there are three options (M,F,do not share) and all three of them are passing the if argument. if i change it to $gender==M then none of them pass. also i doubled checked to make sure that $gender was being properly set. i also tried it with multiple quotation/equal sign set ups. here is the code:

Code:
<?php $Gender = $ProfileContent[20]; ?>
    <label>Gender: </label><select name="Gender">
        <option <?php if($Gender = ""){ echo "selected=\"selected\""; } ?> value="">Do Not Share</option>
        <option <?php if($Gender = M){ echo "selected=\"selected\""; } ?> value="M">M</option>
        <option <?php if($Gender = F){ echo "selected=\"selected\""; } ?> value="F">F</option>
    </select>
 

Scoochi2

New Member
Messages
185
Reaction score
0
Points
0
if $'gender = M then the selected="selected" would be echoed for that options in the drop down box. the problem is there are three options (M,F,do not share) and all three of them are passing the if argument. if i change it to $gender==M then none of them pass. also i doubled checked to make sure that $gender was being properly set. i also tried it with multiple quotation/equal sign set ups.
When using an if expression, dual equals must be used. If you only use one (as in $gender = M ) then what will happen is that PHP will assign M to that variable. Thus, it will always return true in addition to changing the variable.
Additionally, all strings must be encapsulated by quotes. For this purpose, you can use either double or single quotes " or '. They are not however identical. Essentially, anything in single quotes will be output as you write it, whereas in double quotes it will be parsed by PHP before output (so for example, any variables will be output as their value in double quotes, where a single quote would output $myvariable).

It is the combination of these factors which I think have been causing you issues. The code below should work for you.
Oh, and whitespace doesn't matter. At all. Except in strings. $variable = 'value' is the same as $variable='value'

PHP:
<?php $Gender = $ProfileContent[20]; ?>
    <label>Gender: </label><select name="Gender">
        <option <?php if($Gender == ''){ echo 'selected="selected"'; } ?> value="">Do Not Share</option>
        <option <?php if($Gender == 'M'){ echo 'selected="selected"'; } ?> value="M">M</option>
        <option <?php if($Gender == 'F'){ echo 'selected="selected"'; } ?> value="F">F</option>
    </select>
 
Last edited:

garrensilverwing

New Member
Messages
148
Reaction score
0
Points
0
well i tried all the changes you suggested, but as i said before i already tried this and now (with the double =) none of the options pass the if statement
 

ah-blabla

New Member
Messages
375
Reaction score
7
Points
0
well i tried all the changes you suggested, but as i said before i already tried this and now (with the double =) none of the options pass the if statement
The double == is the comparison operator for php, = assignment (as already said), so leave that as it is. (Single = definetely won't work). The problem is you shouldn't compare strings directly using $StringA == $StringB, rather you use a function for this:
PHP:
if (strcomp($StringA, $StringB) == 0)
will return true if the two strings are the same.
See: http://us.php.net/manual/en/function.strcmp.php

Here's your code modified to work:
PHP:
<?php $Gender = $ProfileContent[20]; ?>
    <label>Gender: </label><select name="Gender">
        <option <?php if(strcomp($Gender,"") == 0) { echo 'selected="selected"'; } ?> value="">Do Not Share</option>
        <option <?php if(strcomp($Gender,"M") == 0) { echo 'selected="selected"'; } ?> value="M">M</option>
        <option <?php if(strcomp($Gender,"F") == 0) { echo 'selected="selected"'; } ?> value="F">F</option>
    </select>
 

Scoochi2

New Member
Messages
185
Reaction score
0
Points
0
well i tried all the changes you suggested, but as i said before i already tried this and now (with the double =) none of the options pass the if statement
That's strange...

As ah-blabla said, you can use a function to compare as well, but a direct string comparison DOES work.

I executed the following code on my localhost.
PHP:
<?php $Gender = 'M'; ?>
<label>Gender: </label><select name="Gender">
    <option <?php if($Gender == ''){ echo 'selected="selected"'; } ?> value="">Do Not Share</option>
    <option <?php if($Gender == 'M'){ echo 'selected="selected"'; } ?> value="M">M</option>
    <option <?php if($Gender == 'F'){ echo 'selected="selected"'; } ?> value="F">F</option>
</select>
    
<?php $Gender = ''; ?>
<label>Gender: </label><select name="Gender">
    <option <?php if($Gender == ''){ echo 'selected="selected"'; } ?> value="">Do Not Share</option>
    <option <?php if($Gender == 'M'){ echo 'selected="selected"'; } ?> value="M">M</option>
    <option <?php if($Gender == 'F'){ echo 'selected="selected"'; } ?> value="F">F</option>
</select>

<?php $Gender = 'F'; ?>
<label>Gender: </label><select name="Gender">
    <option <?php if($Gender == ''){ echo 'selected="selected"'; } ?> value="">Do Not Share</option>
    <option <?php if($Gender == 'M'){ echo 'selected="selected"'; } ?> value="M">M</option>
    <option <?php if($Gender == 'F'){ echo 'selected="selected"'; } ?> value="F">F</option>
</select>
The output was as follows:
HTML:
 <label>Gender: </label><select name="Gender">
    <option  value="">Do Not Share</option>
    <option selected="selected" value="M">M</option>
    <option  value="F">F</option>
</select>
    
<label>Gender: </label><select name="Gender">
    <option selected="selected" value="">Do Not Share</option>
    <option  value="M">M</option>
    <option  value="F">F</option>
</select>

<label>Gender: </label><select name="Gender">
    <option  value="">Do Not Share</option>
    <option  value="M">M</option>
So yes, what I said before does work. If it doesn't, I can only conclude there is an issue with the server and you should contact tech support.
 
Last edited:

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
well i tried all the changes you suggested, but as i said before i already tried this and now (with the double =) none of the options pass the if statement

Add a line of code:

<?php $Gender = $ProfileContent[20]; ?>
<?php echo "$Gender is " . $Gender . "<br \>" ?>
<label>Gender: </label><select name="Gender">

to show what $Gender is.
 

ah-blabla

New Member
Messages
375
Reaction score
7
Points
0
The difference using strcomp() is that it is binary safe. (Whatever that means...) I'm just used to using methods to compare strings, since that's the only way in C and Java. The other thing I'm thinking is that the input variable isn't capitalised, i.e. you are comparing "m" to "M", which are two different things.

Ps: I had a similar problem where comparing using == didn't work, but strcmp did. Also, you can use === for strict comparison, but that is unlikely to help here.
 
Last edited:

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
<?php $Gender = $ProfileContent[20]; ?>
<label>Gender: </label><select name="Gender">
<option <?php if($Gender = ""){ echo "selected=\"selected\""; } ?> value="">Do Not Share</option>
<option <?php if($Gender = M){ echo "selected=\"selected\""; } ?> value="M">M</option>
<option <?php if($Gender = F){ echo "selected=\"selected\""; } ?> value="F">F</option>
</select>


If all you did was change '=' to '==' you are going to have another problem.

Put M and F in quotes.
 
Last edited:

ah-blabla

New Member
Messages
375
Reaction score
7
Points
0
I was doing some more research and discovered:
http://www.htmlforums.com/archive/index.php/t-25029.html
The gist of this is you could have extra escape characters in your string, e.g. a new line etc, since the string you are checking is coming from somewhere external, i.e. an Array. Try trim($String) around the input string.

BTW, are you sure you're getting the correct data from the array? In php I think array items are number from 0, so number 20 is the 21st element.
 
Last edited:

garrensilverwing

New Member
Messages
148
Reaction score
0
Points
0
Ah yes $Gender = trim($Gender); worked now its working perfectly, thanks a lot i would have never thought of that
 
Top