7 days of WordPress plugins, themes & templates - for free!* Unlimited asset downloads! Start 7-Day Free Trial
FREELessons: 13Length: 1.4 hours

Next lesson playing in 5 seconds

  • Overview
  • Transcript

3.3 Other Operation Implementations

Now it's time to compare notes. At the end of the last lesson, I asked you finish the implementations for the subtraction, multiplication, and division operations. To start off with, I'll show you my implementation so you can compare it to your own.

3.3 Other Operation Implementations

Okay, hopefully you've taken a little bit of time to implement the subtract, multiply, and divide operations in a similar mechanism to how you watched me implement the add operation. So let's go ahead and see what I've done so far, and then we'll compare notes and we'll go through a couple of example scenarios. So here we have the routing.yml file. As you can see here I've added in some routes. So I had the add already, and I simply added and subtract, multiply and divide. And they're really all the same with the exception of I now have the subtract, multiply, and divide routes defined in my paths. And then, I also have defined in my calculator controller the corresponding subtract, multiply, and divide function. So let's take a look at those and see what those look like. So as you can see here now, I have the corresponding functions for subtract, multiply, and divide. And the only difference is a little bit of terminology. So for subtraction, I have the difference is equal to the first digit minus the second digit. And then within the text as well I talk about the difference instead of the sum. Same thing for multiply. So, I'm calculating the product is the first digit times the second digit. And then finally, divide. We're talking about the quotient, and the quotient is the first digit divided by the second digit. And the rest is really just terminology within the text. So that's the basics of the calculator controller implementation. And then finally, for the form, a lot of similarities here as well. So we are still switching on the op that we pulled out of our form state. But now we handle subtract, multiply, and divide where we're setting the redirect to the corresponding routes that we've defined earlier, passing in first digit and second digit. So now, if you've gone in and you've refreshed you cache or cleared your caches, and start to play around with this a little bit, you should see a functioning example. So let's say we need something like 5, and we subtracted 3, and hit Submit. I should see 2, which I do, so that's good. Now, if we came back here and did the same thing, instead of subtracting, we did multiplying, I should get 15, and I do so that's good. And then, finally, for divide, I should get a long decimal number here, which is good, because I'm dividing two whole numbers that do not divide evenly into each other. So I should get some sort of decimal here. So that's basically what I would expect. So, if you came up with something similar to that, then I applaud you. And if you didn't, that's okay. You still got to the same end that we're looking for, then it's a success. And if you didn't, then go ahead and take a look at the source code that we have up on GitHub for this particular course, and see how I have, maybe, done something a little bit different than you have. Now before we draw this course to a close, I want to point out a couple of things that you might want to pay attention to. So let's go ahead and head back here. Now, we've made some assumptions to this point of what's being input into these text boxes. So let's just say, for example, I was doing a divide operation, and I just so happen to put a zero in here. And we should all know to this point that any sort of division operation where we are dividing a number by zero is typically a bad thing. So you would kind of expect that if I were to hit Submit here, I should get some sort of an error. But PHP is smart enough to say I know that you are trying to divide something by zero, and instead of just blowing up and throwing exceptions all over the place, PHP will actually treat that as infinity, basically an infinite number. So this is a little bit strange, I think. So this might be something that we want to handle, maybe in a validation step to say if it's a divide operation and the second digit is zero. Maybe handle that in some sort of an appropriate fashion. Also, if you come back in here to your calculator and you happen to change one of these values into a letter, so let's say I wanted to divide H by zero and I try to submit that. The whole thing explodes. And you can go in and take a look at the logs in this case. But I'm pretty certain that it's gonna say something to the effect of you can't divide a character or a string by an integer value. Something along those lines. So there's a couple things that we need to do here before we've really wrapped this up. One is, we should probably properly handle the scenario where a user tries to divide some number by zero. So that's the first one. And the second one, we should really enforce that the values that are being submitted in the form need to be integers and not letters. Or they at least need to be numeric and not some sort of string or character, or some other strange thing like that. So in the next lesson, we're gonna handle some custom validations within our form.

Back to the top