Posted By

theworldofdan on 07/21/11


Tagged


Versions (?)

Shorthand IF/ELSE statements


 / Published in: PHP
 

  1. <?php
  2. // Shorthand notation for if/else statements, basic example below:
  3. $myGender == 'male' ? 'i am male' : 'I am female'
  4. ?>
  5.  
  6. <!-- can be used inline, such as: -->
  7. <select name="gender">
  8. <option value="female" <?php echo ($myGender == 'female' ? 'selected' : ''); ?>>Female</option>
  9. <option value="male" <?php echo ($myGender == 'male' ? 'selected' : ''); ?>>Male</option>
  10. </select>
  11.  
  12. <?php
  13. // it can also be used to assign a value to a variable:
  14. $is_admin = ($user['permissions'] == 'admin' ? true : false);
  15. // or:
  16. $x = ($myvalue == 10) ? 'the value is 10' : 'the value is not 10';
  17. ?>

Report this snippet  

You need to login to post a comment.