Strange IF behaviour

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • BWGames

    Strange IF behaviour

    $nst is definitely set to 4, as the echo $nst; gives 4.

    However, with:

    echo $nst;
    echo "<br>";
    if ($nst= 1) {
    $fulls = "Full status for 1";
    } elseif ($nst = 2) {
    $fulls = "Full status for 2";
    } elseif ($nst= 4) {
    $fulls = "Full status for 4";
    }

    echo "Status: " . $fulls;

    $fulls is *always* the first one "Full status for 1", even when $nst is 4.

    What am I doing wrong?

    Thanks,

    Ben
  • Jon Kraft

    #2
    Re: Strange IF behaviour

    google@bwgames. net (BWGames) wrote:
    [color=blue]
    > $nst is definitely set to 4, as the echo $nst; gives 4.
    >
    > However, with:
    >
    > echo $nst;
    > echo "<br>";
    > if ($nst= 1) {
    > $fulls = "Full status for 1";
    > } elseif ($nst = 2) {
    > $fulls = "Full status for 2";
    > } elseif ($nst= 4) {
    > $fulls = "Full status for 4";
    > }
    >
    > echo "Status: " . $fulls;
    >
    > $fulls is *always* the first one "Full status for 1", even when $nst
    > is 4.[/color]

    You are assigning the values, not comparing ;)

    if ($nst == 1) {

    HTH;
    JOn

    Comment

    • Berislav Lopac

      #3
      Re: Strange IF behaviour

      Jon Kraft wrote:[color=blue]
      > google@bwgames. net (BWGames) wrote:
      >[color=green]
      >> $nst is definitely set to 4, as the echo $nst; gives 4.
      >>
      >> However, with:
      >>
      >> echo $nst;
      >> echo "<br>";
      >> if ($nst= 1) {
      >> $fulls = "Full status for 1";
      >> } elseif ($nst = 2) {
      >> $fulls = "Full status for 2";
      >> } elseif ($nst= 4) {
      >> $fulls = "Full status for 4";
      >> }
      >>
      >> echo "Status: " . $fulls;
      >>
      >> $fulls is *always* the first one "Full status for 1", even when $nst
      >> is 4.[/color]
      >
      > You are assigning the values, not comparing ;)
      >
      > if ($nst == 1) {[/color]

      Also, this is better solved by switch:

      switch($nst) {
      case 1 :
      $fulls = "Full status for 1";
      break;
      case 2 :
      $fulls = "Full status for 2";
      break;
      case 4 :
      $fulls = "Full status for 4";
      break;
      }


      --
      If the Internet is a Marx Brothers movie, and Web, e-mail, and IRC are
      Groucho, Chico, and Harpo, then Usenet is Zeppo.


      Comment

      Working...