Skip to content

Instantly share code, notes, and snippets.

@corsonr
Created June 15, 2012 07:37
Show Gist options
  • Save corsonr/2935229 to your computer and use it in GitHub Desktop.
Save corsonr/2935229 to your computer and use it in GitHub Desktop.
Nice selected() fucntion
<!-- Testing the values with if() -->
<select name="options[foo]">
<option value="1" <?php if ( $options['foo'] == 1 ) echo 'selected="selected"'; ?>>1</option>
<option value="2" <?php if ( $options['foo'] == 2 ) echo 'selected="selected"'; ?>>2</option>
<option value="3" <?php if ( $options['foo'] == 3 ) echo 'selected="selected"'; ?>>3</option>
</select>
<!-- Using selected() instead -->
<select name="options[foo]">
<option value="1" <?php selected( $options['foo'], 1 ); ?>>1</option>
<option value="2" <?php selected( $options['foo'], 2 ); ?>>2</option>
<option value="3" <?php selected( $options['foo'], 3 ); ?>>3</option>
</select>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment