Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-17-2012, 02:02 AM   PM User | #1
azoundria
New to the CF scene

 
Join Date: Apr 2012
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
azoundria is an unknown quantity at this point
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 />";
  } 
azoundria is offline   Reply With Quote
Old 11-17-2012, 02:38 AM   PM User | #2
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,667
Thanks: 46
Thanked 456 Times in 444 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
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 --
__________________
Please don't be rude: Put your php code in [php][/php] tags. It is a sticky topic at the top of the forum and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is offline   Reply With Quote
Old 11-17-2012, 04:30 AM   PM User | #3
tracknut
Regular Coder

 
Join Date: Aug 2006
Posts: 907
Thanks: 4
Thanked 212 Times in 211 Posts
tracknut is an unknown quantity at this point
Actually I think what you meant to do was:
Code:
 for ($i = 1; $i < 10; $i<<=1) {
    echo $i."<br />";
  }
Dave
tracknut is online now   Reply With Quote
Old 11-17-2012, 05:40 AM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,752
Thanks: 4
Thanked 2,468 Times in 2,437 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:27 AM.


Advertisement
Log in to turn off these ads.