Shorthand IF/ELSE statements


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.