Skip to content

Instantly share code, notes, and snippets.

@twokul
Forked from getify/ex1.php
Created August 12, 2012 15:23
Show Gist options
  • Save twokul/3332270 to your computer and use it in GitHub Desktop.
Save twokul/3332270 to your computer and use it in GitHub Desktop.
exploring inline decision-logic in templates
<!-- here's the PHP'ish way of making inline decisions while constructing HTML markup -->
<select name="foobar">
<option value="bam" <?=($foobar==="bam"?"selected":"")?>>Bam</option>
<option value="baz" <?=($foobar==="baz"?"selected":"")?>>Baz</option>
</select>
<input type="radio" name="foobar" value="bam" <?=($foobar==="bam"?"checked":"")?>> Bam
<input type="radio" name="foobar" value="baz" <?=($foobar==="baz"?"checked":"")?>> Baz
<!-- here's another PHP'ish approach using parameterized function calling -->
<?php
function selected($val,$test){ return ($val===$test?"selected":""); }
function checked($val,$test){ return ($val===$test?"checked":""); }
?>
<select name="foobar">
<option value="bam" <?=selected($foobar,"bam")?>>Bam</option>
<option value="baz" <?=selected($foobar,"baz")?>>Baz</option>
</select>
<input type="radio" name="foobar" value="bam" <?=checked($foobar,"bam")?>> Bam
<input type="radio" name="foobar" value="baz" <?=checked($foobar,"baz")?>> Baz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment