note 114977 added to function.empty

From: Date: Thu, 08 May 2014 16:16:36 +0000
Subject: note 114977 added to function.empty
Groups: php.notes 
Request: Send a blank email to php-notes+get-199485@lists.php.net to get a copy of this message
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. ---- Server IP: 160.75.25.15 Probable Submitter: 188.119.21.18 ---- Manual Page -- http://php.net/manual/en/function.empty.php Edit -- https://master.php.net/note/edit/114977 Del: integrated -- https://master.php.net/note/delete/114977/integrated Del: useless -- https://master.php.net/note/delete/114977/useless Del: bad code -- https://master.php.net/note/delete/114977/bad+code Del: spam -- https://master.php.net/note/delete/114977/spam Del: non-english -- https://master.php.net/note/delete/114977/non-english Del: in docs -- https://master.php.net/note/delete/114977/in+docs Del: other reasons-- https://master.php.net/note/delete/114977 Reject -- https://master.php.net/note/reject/114977 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#199485) next »