If abfrage?!

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • If abfrage?!

    Hallo,

    Folgender Code macht mir Probleme:
    PHP Code:
    echo $userid;
    if (
    $userid=0)
    {
        echo 
    'Ich war hier';
    } else {
        echo 
    'Fehler';

    echo $userid;

    gibt auf dem Bildschirm "0" aus aber in der If-abfrage springt er trotzdem in den else-teil, wieso?

  • #2
    Re: If abfrage?!

    Original geschrieben von XanaX
    Hallo,

    Folgender Code macht mir Probleme:
    [PHP]
    ($userid=0)
    ...weil ($user=0) != ($user == 0) ist! Somit ist Deine Bedingung immer true
    Last edited by gruenspan; 24-10-2006, 21:45.

    Comment


    • #3
      okay danke funktioniert

      Comment


      • #4
        gruenspan - das ist nicht ganz korrekt.
        zwar hast du den fehler erkannt, aber nicht richtig begründet.

        falsch ist die allgemein geläufige annahme, dass eine zuweisung immer zu "true" ausgewertet wird, "weil sie immer erfolgreich ausgeführt wird".

        richtig ist die tatsache, dass eine zuweisung stets den wert ihrer rechten seite hat.

        somit ist
        PHP Code:
        $x ) == 
        eine wahre aussage.

        was bei XanaX passiert, kann man so auswerten:

        PHP Code:
        if ($userid=0
        entspricht
        PHP Code:
        if (0
        und wird von php implizit gecastet nach "false", also steht dort:
        PHP Code:
        if (false
        bzw.
        PHP Code:
        if (false == true
        was offensichtlich immer falsch ist, sodass der else-block ausgeführt wird.

        nachzulesen unter http://de2.php.net/manual/en/language.expressions.php:
        PHP takes expressions much further, in the same way many other languages do. PHP is an expression-oriented language, in the sense that almost everything is an expression. Consider the example we've already dealt with, '$a = 5'. It's easy to see that there are two values involved here, the value of the integer constant '5', and the value of $a which is being updated to 5 as well. But the truth is that there's one additional value involved here, and that's the value of the assignment itself. The assignment itself evaluates to the assigned value, in this case 5. In practice, it means that '$a = 5', regardless of what it does, is an expression with the value 5. Thus, writing something like '$b = ($a = 5)' is like writing '$a = 5; $b = 5;' (a semicolon marks the end of a statement). Since assignments are parsed in a right to left order, you can also write '$b = $a = 5'.

        Comment


        • #5
          *mähhähä* *wasbinichdochnurfüreinschaf*
          EDIT:
          [COLOR=darkblue]hätt ich ma besser hingeschaut...[/COLOR]
          Last edited by gruenspan; 25-10-2006, 09:57.

          Comment

          Working...
          X