Variable wird falsch gesetzt

Lil-rich

Erfahrenes Mitglied
Hallo Leute,

bin da über einen etwas merkwürdigen Fehler gestolpert, den ich mir irgendwie noch nicht so recht erklären kann... Wahrscheinlich sehe ich den Wald vor lauter Bäumen nicht...

Folgender Code (unverändert):
PHP:
$postfix1 = $this->postfix;
$postfix2 = $postfix;

$allAditions=trim($this->addition." ".
    (!empty($postfix1)?"(".$postfix1.")":
        !empty($postfix2)?"(".$postfix2.")":""));

LoggingClass::log("DEBUG: \$postfix1: ".$postfix1."; \$postfix2: ".$postfix2."; \$allAditions: ".$allAditions."; !empty(\$postfix1): ".(!empty($postfix1))."; !empty(\$postfix2): ".(!empty($postfix2)), LoggingClass::DEBUG);

Folgender Output (unverändert):
DEBUG: $postfix1: FW8; $postfix2: FW4; $allAditions: (FW4); !empty($postfix1): 1; !empty($postfix2): 1

Dem aufmerksamen Leser ist es schon ins Auge gesprungen... Nach der o.g. Logik sollte in $allAdditions eigentlich "(FW8)" stehen - es steht aber "(FW4)" darin?
Irgendwie steh ich auf dem Schlauch... Kann mir einer erklären, warum dieses Phänomen auftritt?
Der Ternary Op. ist doch "if?true:false" und damit sollte, wenn $postfix1 NICHT leer ist (also befüllt) auch direkt $postfix1 zurückgegeben werden?!


Edit:
Hab nun nochmal ein wenig mehr probiert und folgender Code funktioniert interessanterweise problemlos:
PHP:
$postfix1 = $this->postfix;
$postfix2 = $postfix;

if (!empty($postfix1)) {
    $postfix_c = "(".$postfix1.")";
} else if (!empty($postfix2)) {
    $postfix_c = "(".$postfix2.")";
} else {
    $postfix_c = "";
}

$allAditions=trim($this->addition." ".$postfix_c);

?!
 
Zuletzt bearbeitet:
Setze den letzten Ausdruck auch in Klammern.
PHP:
$allAditions=trim(
    $this->addition." ".
    (
        !empty($postfix1) 
        ? "($postfix1)"
        :(
            !empty($postfix2) 
            ? "($postfix2)" 
            : ""
        )
    )
);
 
Zurück