435,061 Members | 1,563 Online
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 function power_of(\$base, \$exponent) {     \$result = \$base;     switch (\$exponent)     {         case \$exponent = 0:             \$result = 1;             break;         case  \$exponent > 1:             while (\$exponent > 1)                 {                 \$result = \$result * \$base;                 \$exponent--;                 }             break;         case  \$exponent < -1:             while (\$exponent < -1)                 {                 \$result = \$result * \$base;                 \$exponent++;                 }             \$result = 1 / \$result;             break;         }     return \$result; }   Any Ideas why that is? Jan 25 '12 #1
8 Replies

 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

 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

 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     switch (\$operator)     {         case 'add':             \$result = \$input1 + \$input2;             break;         case 'sub':             \$result = \$input1 - \$input2;             break;         case 'mult':             \$result = \$input1 * \$input2;             break;         case 'idiv':             \$result = \$input1 / \$input2;             \$result = (integer)\$result;             break;         case 'div':             \$result = \$input1 / \$input2;             break;         case 'modulus':             \$result = \$input1 % \$input2;             break;         case 'sqrt' :             \$result = babylonian_square_root(\$input1);             break;         case 'power_of' :             \$result = power_of(\$input1, \$input2);             break;         case 'factorial' :             \$result = factorial(\$input1);             break;           default:             \$result = INVALID_OPERATOR;             break;     }     if (\$radix == 'hex')     {         \$result = decimal_to_hex(\$result, true);         \$start_again_radix = '?hex';     } }     else      \$result = INVALID_INPUT;   \$operator \$input1 \$input2 are all \$_POST from another php file. I changed line 6 in P1 to: Expand|Select|Wrap|Line Numbers \$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     switch (\$operator)     {         case 'add':             \$result = \$input1 + \$input2;             break;         case 'sub':             \$result = \$input1 - \$input2;             break;         case 'mult':             \$result = \$input1 * \$input2;             break;         case 'idiv':             \$result = \$input1 / \$input2;             \$result = (integer)\$result;             break;         case 'div':             \$result = \$input1 / \$input2;             break;         case 'modulus':             \$result = \$input1 % \$input2;             break;         case 'sqrt' :             \$result = babylonian_square_root(\$input1);             break;         case 'power_of' :             \$result = power_of(\$input1, \$input2);             break;         case 'factorial' :             \$result = factorial(\$input1);             break;           default:             \$result = INVALID_OPERATOR;             break;     }     if (\$radix == 'hex')     {         \$result = decimal_to_hex(\$result, true);         \$start_again_radix = '?hex';     } }     else      \$result = INVALID_INPUT;   \$operator \$input1 \$input2 are all \$_POST from another php file. I changed line 6 in P1 to: Expand|Select|Wrap|Line Numbers case \$exponent == 0: Still does not take the branch it should. Jan 26 '12 #8

 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