PHP coding

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • wisni1rr
    New Member
    • Nov 2011
    • 78

    PHP coding

    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.

    Code:
    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?
  • brettl
    New Member
    • Sep 2007
    • 41

    #2
    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.

    Comment

    • Rabbit
      Recognized Expert MVP
      • Jan 2007
      • 12517

      #3
      = is an assignment operator. == is a comparison operator.

      Comment

      • wisni1rr
        New Member
        • Nov 2011
        • 78

        #4
        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.

        Comment

        • Rabbit
          Recognized Expert MVP
          • Jan 2007
          • 12517

          #5
          That will depend on how you're calling the function.

          Comment

          • macoy23
            New Member
            • Jan 2012
            • 2

            #6
            The 1st case should be $exponent == 0: NOT $exponent =0:

            Comment

            • wisni1rr
              New Member
              • Nov 2011
              • 78

              #7
              The function is called in this statement:
              Code:
              	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:
              Code:
              $exponent == 0
              Still does not take the branch it should.

              Comment

              • wisni1rr
                New Member
                • Nov 2011
                • 78

                #8
                The function is called in this statement:
                Code:
                	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:
                Code:
                case $exponent == 0:
                Still does not take the branch it should.

                Comment

                • Rabbit
                  Recognized Expert MVP
                  • Jan 2007
                  • 12517

                  #9
                  I would output the values to double check that the correct values are getting passed.

                  Comment

                  Working...