PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
I know that I could probably use eval() for this, but that is probably too dangerous. In the future I would like to turn it into a pretty cool app, where they can add parenthesis, etc.
Would anyone happen to know how I could create this?
Thanks!
jaoudestudios wrote:I would do it using php, no need to drop into the shell. Once you have it working in php, then you can progressively enhance it with javascript
Indeed. I was planning on doing it in PHP. But I don't know where to start.
In the example I gave just put the conditions (add/subtract etc...) for each in the conditional statement and do the calculation. I would use a switch not elseif.
In the example I gave just put the conditions (add/subtract etc...) for each in the conditional statement and do the calculation. I would use a switch not elseif.
That would be silly. Once users start to use parenthesis, or I add modulus then it would become code spaghetti.
In the example I gave just put the conditions (add/subtract etc...) for each in the conditional statement and do the calculation. I would use a switch not elseif.
That would be silly. Once users start to use parenthesis, or I add modulus then it would become code spaghetti.
I thought you wanted it simple especially as you had nothing working yet. You can do it object orientated if you want. Create a calculation class with a method for each type of calculation.
I would start with a base class that has simple calculations (+,-,/,x), then extend (or however you prefer to do it) with additional modules (sin,cos,tan etc) one at a time and test as you go along, otherwise it will become messy. Get each module working then tidy it up before going on to the next one. Also if you have problems with a module you can just post that module here and we can help.
jaoudestudios wrote:I thought you wanted it simple especially as you had nothing working yet. You can do it object orientated if you want. Create a calculation class with a method for each type of calculation.
Ok. How would I go about creating a class that could handle, for instance:
jaoudestudios wrote:How would the user add that info to your online calculator with 2 input fields? (num1 & num2)
It would actually be using javascript to dynamically create elements, such as a opening parenthesis, a number, an addition/subtraction/division/multiplication/etc system, another number and finally a closing parenthesis.
jaoudestudios wrote:How would the user add that info to your online calculator with 2 input fields? (num1 & num2)
It would actually be using javascript to dynamically create elements, such as a opening parenthesis, a number, an addition/subtraction/division/multiplication/etc system, another number and finally a closing parenthesis.
I still recommend to do it with php and progressively enhance it with javascript later on. That way it downgrades gracefully.
I would start by grabbing the inner 2 brackets with a regex and work outwards while keeping a running total. If this is looped it wont matter how many encapsulated brackets there are.
But like I said start simple.
Try something like this in this order...