Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Apr 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Looping Through Exponents

    This works fine as 1, 2, 3, 4, 5, 6, 7, 8, 9:

    PHP Code:
      for ($i 1$i 10$i++) {
        echo 
    $i."<br />";
      } 
    Why does this not work to go 1, 2, 4, 8?

    PHP Code:
      for ($i 1$i 10$i<<1) {
        echo 
    $i."<br />";
      } 

  • #2
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,352
    Thanks
    61
    Thanked 528 Times in 515 Posts
    Quote Originally Posted by azoundria View Post
    PHP Code:
      for ($i 1$i 10$i<<1
    You can't use <<.

    You can only use ++ or --
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #3
    Senior Coder
    Join Date
    Aug 2006
    Posts
    1,341
    Thanks
    11
    Thanked 288 Times in 287 Posts
    Actually I think what you meant to do was:
    Code:
     for ($i = 1; $i < 10; $i<<=1) {
        echo $i."<br />";
      }
    Dave

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Tracknut sums it up.
    The post condition in the for loop must result in something that changes control. The increment and decrement operators are (at least I'm pretty sure) the only two in PHP that alter the state of the variable when applied. The other unary operators like boolean and bitwise nots (! and ~) only work on the returned result of the operation, and likewise the binary operations (ie $i << 1 or $i + 1) would require assignment back to the $i in order to alter it.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •