note 114977 deleted from function.empty by cmb

From: Date: Sun, 01 Dec 2019 09:24:12 +0000
Subject: note 114977 deleted from function.empty by cmb
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-213361@lists.php.net to get a copy of this message
Note Submitter: turabgarip at gmail dot com ---- Checking if a variable is empty or not when you have more than one will add so many similar lines to your code; and will require re-writing of course. Like for example; <?php if (!empty($a)) $myvar = $a; elseif (!empty($b)) $myvar = $b; //... ?> If, for example $myvar should be pulled among 5 vars, you need to write it for 5 times. But in the below example, using $myvar = esor($var1, $var2, ...) will set it to first non-empty var. <?php function esor() { $arg_num = func_num_args(); // "No arguments" is empty string if (!$arg_num) return ''; $args = func_get_args(); for ($i = 0; $i < $arg_num; $i++) if (!empty($args[$i])) return $args[$i]; // Not found any filled var? return ''; // Empty string is what you get } // For example; mail(esor($settings['admin_mail'], $_ENV['SERVER_ADMIN']), 'Something went wrong', 'Error was foo'); ?> This will make it "one line code" no matter how many vars you have to check. Hope that helps.

« previous php.notes (#213361) next »