You may or may not have heard of shorthand if/else statements, but they do exist, and trust me they make things SO much more ordered in your code when used in the right way.

This is the Shorthand version - below this, if you cannot work it out for yourself is a nice description.

    // Will print out ‘i am male’ if $myGender is ‘male’ and vice-versa.
    echo ($myGender == ‘male’ ? ‘i am male’ : ‘I am female’);

Description of how this actually works
We all (i would hope) know the normal way of writing if/else statements (here is a recap just incase)

$myVar = true;
if($myVar) {
    echo ‘My Var is TRUE!’;
} else {
    echo ‘My Var is FALSE’;
}

That sort of statement is great for in the body of your code, but when want a dynamic selector within your HTML code, it gets very messy.

EG:

if($myGender == ‘male’) {
    // Gender is MALE.
    $maleOpt = ’selected’;
    $femaleOpt = ;
} else {
    // Gender is FEMALE.
    $maleOpt = ;
    $femaleOpt = ’selected’;
}
<select name="gender"><
option value="female" <?= $femaleOpt; ?>>Female</option>
<option value="male" <?= $maleOpt; ?>>Male</option>
</select>
 

Now - that is alot to write and will make your HTML form look a total mess, this is where the shorthand comes in. It allows us to write the same thing inline

Look:

<select name="gender">
<option value="female" <?= ($myGender == ‘female’ ? ’selected’ : ”); ?>>Female</option>
<option value="male" <?= ($myGender == ‘male’ ? ’selected’ : ”); ?>>Male</option>
</select>
 

As you can see - there is a huge difference in the amount of code - and if Wordpress formatted it nicely - it would look loads better!

And the all important Explanation!

// This will echo selected to the screen if $myGender is male.
echo ($myGender = ‘male’ ? ‘ selected’ : );
 

So - $myGender is the variable we are checking, in this case we are checking to see if $myGender == ‘male’

The questionmark (?) seperates the ‘answers’.

The first ‘answer’ is what is set/displayed when the query is true, the second is if the query is false.

This is the equilivent to :

if($myGender == ‘male’) {
    echo ’selected’;
} else {
    echo ;
}

Hope that helps some of you out!