473,324 Members | 2,531 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

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.

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
8 1714
brettl
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
12,516 Expert Mod 8TB
= is an assignment operator. == is a comparison operator.
Jan 25 '12 #3
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
12,516 Expert Mod 8TB
That will depend on how you're calling the function.
Jan 26 '12 #5
The 1st case should be $exponent == 0: NOT $exponent =0:
Jan 26 '12 #6
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
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
12,516 Expert Mod 8TB
I would output the values to double check that the correct values are getting passed.
Jan 27 '12 #9

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

Similar topics

3
by: ganesan | last post by:
Hi Guys, Could any one knows the best coding standards styles(with variable declarations for c#) . and if any links or site with the best coding standards for .NET send me those links regards...
4
by: dotNetDave | last post by:
About three weeks ago I released the first .NET coding standards book titled "VSDN Tips & Tricks .NET Coding Standards". Here is what the famous author/ speaker Deborah Kurata says about it: ...
0
by: Berthold Höllmann | last post by:
I have a default coding header # -*- coding: iso-8859-15 -*- in my python files. I now have Problems with this settings. I swithched to Python 2.4.1 under Windows. When I import files with the...
144
by: Natt Serrasalmus | last post by:
After years of operating without any coding standards whatsoever, the company that I recently started working for has decided that it might be a good idea to have some. I'm involved in this...
7
by: Ralph Lund | last post by:
Hi. I am starting a new project with C#. I am searching for "good" coding conventions. I know that there are some coding conventions from microsoft, (but they are very extensive and not clear)....
13
by: benben | last post by:
Is there an effort to unify the c++ coding standard? Especially identifier naming. Not a big issue but it would be annoying to have to incorporate different coding styles simultaneously when...
7
by: Robert Seacord | last post by:
The CERT/CC has just deployed a new web site dedicated to developing secure coding standards for the C programming language, C++, and eventually other programming language. We have already...
3
by: editormt | last post by:
A recent poll asked if programming standards are used by development organisations... and if they are controlled. None: 20% Yes, but without control: 49% Yes, with control: 31% Participants:...
0
by: pat | last post by:
CodeCheck Coding Standard's Support As a free service to our customers we offer support in developing "rule-files" for automating corporate coding standards. If you have a coding standard that...
8
by: =?ISO-8859-1?Q?Arnaud_Carr=E9?= | last post by:
Hi all, I guess you all know how difficult it is to choose a conding standard. And even more difficult it is to explain the choice to your dev team :-) I'm looking for an "official" c++ coding...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.