By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,061 Members | 1,563 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,061 IT Pros & Developers. It's quick & easy.

PHP coding

P: 78
I am building a calculator program to help learn PHP. In the following example, I wrote a function to calculate exponents. When I run this function, It always takes the first case.

Expand|Select|Wrap|Line Numbers
  1. function power_of($base, $exponent)
  2. {
  3.     $result = $base;
  4.     switch ($exponent)
  5.     {
  6.         case $exponent = 0:
  7.             $result = 1;
  8.             break;
  9.         case  $exponent > 1:
  10.             while ($exponent > 1)
  11.                 {
  12.                 $result = $result * $base;
  13.                 $exponent--;
  14.                 }
  15.             break;
  16.         case  $exponent < -1:
  17.             while ($exponent < -1)
  18.                 {
  19.                 $result = $result * $base;
  20.                 $exponent++;
  21.                 }
  22.             $result = 1 / $result;
  23.             break;    
  24.     }
  25.     return $result;
  26. }
  27.  
Any Ideas why that is?
Jan 25 '12 #1
Share this Question
Share on Google+
8 Replies


brettl
P: 41
What is the value of $exponent? If it has no value it may be validating it as false that's why only the first case will work.
Jan 25 '12 #2

Rabbit
Expert Mod 10K+
P: 12,366
= is an assignment operator. == is a comparison operator.
Jan 25 '12 #3

P: 78
Thanks, brettl & Rabbit.

$exponent and $base are retrieved from $_POST().

I added {==} to Line 6 of the code in post #1.

If I enter 500 for $base and 0 for $exponent, I get the $result of 500.
Jan 25 '12 #4

Rabbit
Expert Mod 10K+
P: 12,366
That will depend on how you're calling the function.
Jan 26 '12 #5

P: 2
The 1st case should be $exponent == 0: NOT $exponent =0:
Jan 26 '12 #6

P: 78
The function is called in this statement:
Expand|Select|Wrap|Line Numbers
  1.     switch ($operator)
  2.     {
  3.         case 'add':
  4.             $result = $input1 + $input2;
  5.             break;
  6.         case 'sub':
  7.             $result = $input1 - $input2;
  8.             break;
  9.         case 'mult':
  10.             $result = $input1 * $input2;
  11.             break;
  12.         case 'idiv':
  13.             $result = $input1 / $input2;
  14.             $result = (integer)$result;
  15.             break;
  16.         case 'div':
  17.             $result = $input1 / $input2;
  18.             break;
  19.         case 'modulus':
  20.             $result = $input1 % $input2;
  21.             break;
  22.         case 'sqrt' :
  23.             $result = babylonian_square_root($input1);
  24.             break;
  25.         case 'power_of' :
  26.             $result = power_of($input1, $input2);
  27.             break;
  28.         case 'factorial' :
  29.             $result = factorial($input1);
  30.             break;
  31.  
  32.         default:
  33.             $result = INVALID_OPERATOR;
  34.             break;
  35.     }
  36.     if ($radix == 'hex')
  37.     {
  38.         $result = decimal_to_hex($result, true);
  39.         $start_again_radix = '?hex';
  40.     }
  41. }    
  42. else 
  43.     $result = INVALID_INPUT;
  44.  
$operator $input1 $input2 are all $_POST from another php file.

I changed line 6 in P1 to:
Expand|Select|Wrap|Line Numbers
  1. $exponent == 0
Still does not take the branch it should.
Jan 26 '12 #7

P: 78
The function is called in this statement:
Expand|Select|Wrap|Line Numbers
  1.     switch ($operator)
  2.     {
  3.         case 'add':
  4.             $result = $input1 + $input2;
  5.             break;
  6.         case 'sub':
  7.             $result = $input1 - $input2;
  8.             break;
  9.         case 'mult':
  10.             $result = $input1 * $input2;
  11.             break;
  12.         case 'idiv':
  13.             $result = $input1 / $input2;
  14.             $result = (integer)$result;
  15.             break;
  16.         case 'div':
  17.             $result = $input1 / $input2;
  18.             break;
  19.         case 'modulus':
  20.             $result = $input1 % $input2;
  21.             break;
  22.         case 'sqrt' :
  23.             $result = babylonian_square_root($input1);
  24.             break;
  25.         case 'power_of' :
  26.             $result = power_of($input1, $input2);
  27.             break;
  28.         case 'factorial' :
  29.             $result = factorial($input1);
  30.             break;
  31.  
  32.         default:
  33.             $result = INVALID_OPERATOR;
  34.             break;
  35.     }
  36.     if ($radix == 'hex')
  37.     {
  38.         $result = decimal_to_hex($result, true);
  39.         $start_again_radix = '?hex';
  40.     }
  41. }    
  42. else 
  43.     $result = INVALID_INPUT;
  44.  
$operator $input1 $input2 are all $_POST from another php file.

I changed line 6 in P1 to:
Expand|Select|Wrap|Line Numbers
  1. case $exponent == 0:
Still does not take the branch it should.
Jan 26 '12 #8

Rabbit
Expert Mod 10K+
P: 12,366
I would output the values to double check that the correct values are getting passed.
Jan 27 '12 #9

Post your reply

Sign in to post your reply or Sign up for a free account.