PHP bug oder dummheit? [x] dummheit!

heyho

kann mir jemand mal das verhalten erklären? überseh ich irgendwas?
oder wieso kommt nicht bei beiden "erstellt desc" raus?

PHP:
<?php
class test {
    public $data = array();
    public function __construct() {
      
      $this->data["sort"] = "erstellt";
      $this->data["order"] = "1";
      
      $order = null;
      if (isset($this->data["sort"])) {
          $order = $this->data["sort"];
          if (isset($this->data["order"])) $order .= ' ' . ($this->data["order"] == 1) ? "desc" : "asc";
      }
      echo $order;
      //returns erstelltdesc

      echo "------------------------------\n";
      
      $order = null;
      if (isset($this->data["sort"])) {
          $order = $this->data["sort"];
          if (isset($this->data["order"]))  {
              $order .= ' ';
              $order .= ($this->data["order"] == 1) ? "desc" : "asc";
          }
      }
      echo $order;
      //returns erstellt desc
    }
}
new test()
?>
 
Zuletzt bearbeitet:
ich hat das mal reportet um zu gucken was die php jungs zu sagen:

Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

Please see php.net7operators for the Operator Precedence.What happensis
that in



$order .= ' ' . ($this->data["order"] == 1) ? "desc" : "asc";



The expression



' ' . ($this->data["order"] == 1)



will be evaluated first. Depending on the result "desc" or"asc" will be
evaluated and returned.



Use parenthesis:



$order .= ' ' . (($this->data["order"] == 1) ? "desc" : "asc");

das macht sinn und is daher in meiner config richtig, aber wieso macht deine php5.3.1 was anderes draus?
 
Ja, das macht wirklich sinn. Ich kann Dir auch sagen warum es bei mir ging, ich habe Deinen Code angepasst. Ich schreibe den Ternary Operator immer in Klammern oder mache den in einer seperaten Zeile.
 
Zurück
Oben