note 33897 added to control-structures.while

From: Date: Thu, 10 Jul 2003 13:39:41 +0000
Subject: note 33897 added to control-structures.while
Groups: php.notes 
Request: Send a blank email to php-notes+get-51845@lists.php.net to get a copy of this message
Here's something really cool and simple. Using the while loop, you can calculate exponents (powers). The code looks like this: <?php //Since the values of $a and $b will be changing, //we need to assign new variables to them, $c and $d. $c = $a; $d = $b; while ($b>1) { //Here we're changing the value of $a. $a = ($a*$c); //Here we're changing the value of $b $b--; } //And to top it all off, a little statement at the end. print (" $c<sup>$d</sup> &#61; $a"); ?> This code works because we are subtracting 1 from the value of $b each time through the loop. Since we're saying that $a = ($a*$c), we are changing the value of $a but not the value of $c. We need the variables $c and $d for the statement at the end because the values of $a and $b have changed. This is a really simple trick that most of you could have figured out in a matter of seconds, but it is a good trick for beginners like me. ---- Manual Page -- http://www.php.net/manual/en/control-structures.while.php Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+33897 Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+33897&report=yes Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+33897&report=yes

« previous php.notes (#51845) next »