kain: In ein echo eine if anweisung verbauen?

Hallo,

ich möchte gerne in einem echo eine if Anweisung einbauen.

echo"<option".if($tarife['tarifname'] == $grund2['tarifname'])echo" selected".">".$tarife['tarifname']."</option>"

Fehlermeldung:

arse error: syntax error, unexpected T_IF in /mnt/....

Geht das überhaupt, was mache ich falsch?

mfg kain

  1. Hallo,

    erledigt

    echo"<option"; if($tarife['tarifname'] == $grund2['tarifname'])echo" selected";echo">".$tarife['tarifname']."</option>";

    mfg kain

  2. Hallo,

    ich möchte gerne in einem echo eine if Anweisung einbauen.

    echo"<option".if($tarife['tarifname'] == $grund2['tarifname'])echo" selected".">".$tarife['tarifname']."</option>"

    also ich mache das in solchen Fällen dann so:

      
    if($tarife['tarifname'] == $grund2['tarifname']) echo '<option selected="selected">'.$tarife['tarifname'].'</option>';  
    else echo '<option>'.$tarife['tarifname'].'</option>';  
    
    

    Vielleicht nicht die eleganteste Lösung - funktioniert aber dafür.

    Gruß Gunther

  3. Hi,

    ich möchte gerne in einem echo eine if Anweisung einbauen.

    echo"<option".if($tarife['tarifname'] == $grund2['tarifname'])echo" selected".">".$tarife['tarifname']."</option>"

    echo "<option".([link:http://www.php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary@title=$tarife['tarifname'\] == $grund2['tarifname'\] ? " selected" : ""]).">".$tarife['tarifname']."</option>";

    MfG ChrisB

    --
    Light travels faster than sound - that's why most people appear bright until you hear them speak.
  4. Hi!

    Noch ein Lösungsvorschlag:

    echo"<option".if($tarife['tarifname'] == $grund2['tarifname'])echo" selected".">".$tarife['tarifname']."</option>"

    printf('<option %s>%s</option>',
      $tarife['tarifname'] == $grund2['tarifname'] ? 'selected' : '',
      htmlspecialchars($tarife['tarifname']));

    Kontextwechsel bei Ausgaben beachten!

    Lo!