Quick questions

Discussion in 'PHP' started by getatune, Feb 17, 2010.

  1. #1
    So I have

    $x = 2;

    But I added $x-1; it wont subtract 1 from the variable. Can someone help me here. Am I doing this wrong?
     
    getatune, Feb 17, 2010 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    If you're always looking to subtract just 1, you can use --.

    $variable = 5;
    $variable = $variable--; //Now 4
    $variable = $variable - 1; //Also 4
     
    jestep, Feb 17, 2010 IP
  3. getatune

    getatune Well-Known Member

    Messages:
    1,360
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    125
    #3
    For some strange reason I have $x=2 at the top and I put in a certain area: $x= $x-1; and it doesnt subtract 1
     
    getatune, Feb 17, 2010 IP
  4. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Then you have done something else wrong. Why not post all the code? Or, in the privacy of your own home, simply stick in a bunch of:

    echo "<p>x is now {$x}</p>";

    throughout the code and then watch the progress of $x to see where it diverges from your expectations.
     
    SmallPotatoes, Feb 17, 2010 IP
  5. getatune

    getatune Well-Known Member

    Messages:
    1,360
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    125
    #5
    at the top I have
    <?
    $x = 2;
    INCLUDES....
    ?>
    Then I have a function and in that function it will display text

    $x = $x -1;
    echo "fiefieh"

    It shows fiefieh

    at the footer I have
    <?=$x?>
    and it shows 2 whats wrong
     
    getatune, Feb 17, 2010 IP
  6. Colbyt

    Colbyt Notable Member

    Messages:
    3,224
    Likes Received:
    185
    Best Answers:
    0
    Trophy Points:
    210
    #6
    Try $x= ($x-1);
    echo $x;

    And the result should be the assigned value of X minus 1.
     
    Colbyt, Feb 17, 2010 IP
  7. getatune

    getatune Well-Known Member

    Messages:
    1,360
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    125
    #7
    nothing changed
     
    getatune, Feb 17, 2010 IP
  8. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #8
    echoing "fiefieh" is not interesting or useful. echo $x.
     
    SmallPotatoes, Feb 18, 2010 IP