empty aber alles

k3nguruh

Erfahrenes Mitglied
Hallo,

da "empty()" anscheinend nicht wirklich nur leere Resultate als leer erkennt habe ich mal was zusammen gebastelt. Scheint zu funktionieren. Aber gibt es vll eine elegantere Lösung?

Funktion:
PHP:
function isEmpty($value)
{
  if (is_null($value)) {
    return true;
  } elseif (is_bool($value) && $value === false) {
    return true;
  } elseif (is_array($value) && count($value) === 0) {
    return true;
  } elseif (is_int($value) && strlen($value) === 0) {
    return true;
  } elseif (is_string($value) && strlen(trim($value)) === 0) {
    return true;
  }

  return false;
}

PHP:
$arr = [
  "Bool = true" => true,
  "Bool = false" => false,
  "Float = 0.00" => 0.0,
  "Float = 8.88" => 8.88,
  "Number = 0" => 0,
  "Number = 8" => 8,
  'Number = "0"' => "0",
  'Number = "8"' => "8",
  'String = ""' => "",
  'String = " "' => " ",
  'String = "string"' => "string",
  "Array = []" => [],
  'Array = ["array"]' => ["array"],
  "Null = null" => null,
];


foreach ($arr as $key => $value) {
  if (isEmpty($value)) {
    echo "{$key} IS Empty<br/>";
  } else {
    echo "{$key} NOT Empty<br/>";
  }
}


function isEmpty($value)
{
  if (is_null($value)) {
    return true;
  } elseif (is_bool($value) && $value === false) {
    return true;
  } elseif (is_array($value) && count($value) === 0) {
    return true;
  } elseif (is_int($value) && strlen($value) === 0) {
    return true;
  } elseif (is_string($value) && strlen(trim($value)) === 0) {
    return true;
  }

  return false;
}


Ergebnis:
Code:
Bool    =   true        NOT Empty
Bool    =   false       IS Empty
Float   =   0.00        NOT Empty
Float   =   8.88        NOT Empty
Number  =   0           NOT Empty
Number  =   8           NOT Empty
Number  =   "0"         NOT Empty
Number  =   "8"         NOT Empty
String  =   ""          IS Empty
String  =   " "         IS Empty
String  =   "string"    NOT Empty
Array   =   []          IS Empty
Array   =   ["array"]   NOT Empty
Null    =   null        IS Empty
 
Ich würde noch parameter mitgeben. EInige Tests können je nach Einsatz verschieden gebraucht werden
1) Soll 0 als Empty gewertet werden
2) soll False als Empty gewertet werden
3) soll ein Leerstring als Empty gewertet werden

- empty() würde ich auch noch prüfen
- Anstelle von is_int() würde ich is_numeric() verwenden, um adere Zahlen auch zu testen
- Noch Objekte auf Nothing testen. Dazu das Objekt in einen Array parsen und mit empty testen
- DIe Reihenfolge ist wichtig. is_numeric erkennt " 1" als Zahl. ALso zuerst auf String testen.

Desweiteren kannst du den Else-Teil streichen. "If then exit" reicht

PHP:
//Parameter Enum. Ab PHP 8.1 gibt es glaub auch eie Enum-Klasse
define('IE_NO_PARAM', 0);
define('IE_ZERO_IS_NULL', 2**0);    //Wert 1
define('IE_FALSE_IS_NULL', 2**1);   //Wert 2
define('IE_SPACE_IS_NULL', 2**2);   //Wert 4
define('IE_DEFAULT', IE_FALSE_IS_NULL+IE_ZERO_IS_NULL); //Wert 2+1 = 3

function isEmpty($value, int $params = IE_DEFAULT){
    if (is_null($value))    return true;
    if (is_array($value))   return count($value) === 0;
    if (is_string($value))  return strlen(andB($params, IE_SPACE_IS_NULL) ? trim($value) : $value) === 0;
    if (is_numeric($value)) return andB($params, IE_ZERO_IS_NULL) ? $value === 0 : false;
    if (is_bool($value))    return andB($params, IE_FALSE_IS_NULL)? $value === false : false;
    if (is_object($value))  return empty((array) $value);
    if (empty($value))      return true;
    return false;
}
/**
 * Bit-Vergleich
 */
function andB(int $haystack, int $needle) :bool{
    return ($haystack & $needle) == $needle;
}

echo '<br />'. var_dump(isEmpty(12));
echo '<br />'. var_dump(isEmpty(0));
echo '<br />'. var_dump(isEmpty(0, IE_NO_PARAM));
echo '<br />'. var_dump(isEmpty(" "));
echo '<br />'. var_dump(isEmpty(" ", IE_SPACE_IS_NULL));
echo '<br />'. var_dump(isEmpty((object) array()));
Ausgabe
Code:
bool(false)
bool(true)
bool(false)
bool(false)
bool(true)
bool(true)
 
Zuletzt bearbeitet:
OK, das einzige was mir dann noch als "mikro"-Optimierung einfällt, wäre den Umstand zu nutzen,
a) dass ein Bool False numerisch 0 ist,
b) dass ein Trim(EinStringNurMitSpaces) die Länge 0 hat (was Yaslaw in seinem Beispiel ja auch macht),
und dass dann an die numerische Prüfung zu geben.
 
Zurück