Find first truthy value


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

Can be used like SQL COALESCE, IFNULL or for defaulting if a value is falsy


Copy this code and paste it in your HTML
  1. function firstTruthy(/*$value1, $value2, $valueN...*/) {
  2. $args = func_get_args();
  3. foreach ($args as $a) {
  4. if (!!$a) {
  5. return $a;
  6. }
  7. }
  8. return null;
  9. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.