From the course: Learning PHP

Arithmetic operators - PHP Tutorial

From the course: Learning PHP

Start my 1-month free trial

Arithmetic operators

- [Instructor] There's no way around it, if you write programs, you will need to do some math. It could be as simple as incrementing and detrimenting or you can do some pretty advanced calculations. Heck, some gaming engines are made partially with PHP. So to start, you can perform the four traditional arithmetic operations. Addition with the plus sign, subtraction with the minus sign, multiplication with the asterix and division with a forward slash. You can perform these operations in variable assignments or directly output them using echo. So we could say, echo one plus one or numb gets one plus one and then echo numb, and in both instances, the result is two. PHP is smart enough to recognize that we are using literal numbers here and not strings. So, it evaluates the statement before the results are outputted. And here are some examples of other operations, we could say A gets two and then echo A plus two, which will output four. We could say B gets three, and then we can say, echo B minus A which will output one. We could say C gets A times B and then echo C, which will output six. And then we could say echo C divided by A, which will output three. You can also negate a value by pretending the minus sign. So like, negative three, so we can say numb gets 20 and then echo minus sign numb and the output would be negative 20. You can even do complex mathematical operations using parentheses or exponents. The rules of PEMDAS will usually apply, and as a refresher, PEMDAS stands for parentheses, exponents, multiplication, and division, and then addition and subtraction. So, let's go out to our code editor now and look at a couple of examples. Though parentheses are technically optional, they are very important to use them in your code to make operations explicit. Let's look at an example where parentheses matter, so we'll open our PHP tag, and then we'll say echo five times six, plus three minus one, so we'll save this and then we'll refresh our page and the answer is 32. Now, if we were to say, echo this statement again, but put parentheses around six plus three, refresh, and they're bumping up against each other so let's add a line break here, and the answer is 44. So again, the parentheses are going to matter, similarly, if we put parentheses around five times six, we should still get 32 there, right? Because the operation is being done in order. And just for one more example, if we do three plus five times six, we'll get 32. So, even in this example, the rules of PEMDAS are applying. And if you want to use exponent, you would do that with two asterisks, so, if we say echo five, asterix, asterix two, that's going to give us five squared, which is going to be 25, I'll get rid of these operators up here, and that's 25. Now, let's look at one more complex equation where we have an exponent included. So, we'll do five asterix, asterix two, times, we'll put the parentheses around six plus three so hopefully that will get done first because that's parentheses, we'll save and we get to 224. If we remove the parentheses, we should get a different answer, 152. So, these are the basic mathematical operators in PHP, but there are a few others that you should be aware of.

Contents