From the course: PHP for Non-Programmers
Math in PHP - PHP Tutorial
From the course: PHP for Non-Programmers
Math in PHP
- [Instructor] PHP allows you to add, subtract, multiply, and divide numbers, as well as perform advanced mathematical operations. And there's no way around using math in PHP. In fact, there have already been examples in previous videos in this course. These are generally referred to as arithmetic operations. First, there are four basic operations, addition with the plus sign, subtraction with the dash or minus sign, multiplication with the asterisk, and division with a forward slash. You can perform these operations in variable assignments or directly output them using echo. For example, we have echo 1 + 1, or we have $num gets 1 + 1, echo $num. In both instances, the result is 2. PHP is smart enough to recognize that we are using literal numbers and not strings. So it evaluates the results before outputting them. Here are some other examples with the rest of the operations. So if we have $a gets 2 and then echo $a + 2, this outputs 4. If we have $b gets 3 and then echo $b - $a, this will output 1. If we have $c gets $a * $b and then echo $c, this will output 6. And then if we have echo $c / $a, this will output 3. There are a few other operations to consider. The first is finding the remainder with the percent sign. Use the percent sign, called modulo or modulus, sometimes mod for short, to return the remainder from dividing two numbers. So in our example, if $a gets 2 and $b gets 3, if we echo $b mod $a, this will print out 1, because three divided by two is one with a remainder of one. And then similarly, if we echo 15 mod 9, this prints out 6. This is often used to figure out if some number is even or odd, or even better, to determine the factors of some number. So if the mod is zero, the first number is a factor of the second number. And again, if we do 4 mod 2, that's going to be a mod of 0, which means it's an even number. Next is complex mathematical operations using parentheses or even exponents. The rules of PEMDAS usually apply here in PHP. And as a refresher, PEMDAS stands for Parentheses, Exponents, Multiplication and Division, and Addition and Subtraction. This is the order of operations. So if we have parentheses, we'll do whatever is in the parentheses first, then we'll evaluate exponents. And then if it's a tie between evaluating multiplication and addition, multiplication will come first, then addition. Using parentheses is extra important here for two reasons. It makes your code more readable, but it also removes ambiguity from PHP, which can sometimes be unpredictable. In the examples we see on the screen here, we have echo 5 * 6 + 3 - 1. That prints 32, which is a proper evaluation. We also have echo 5 times, parenthesis 6 + 3 end parentheses, minus 1. That prints 44, which is a proper evaluation. However, in different versions of PHP, in older versions of PHP, you might get unpredictable results where the plus is always evaluated first or the multiplication is evaluated first. And so we want to make sure that parentheses are appropriately being applied here. Finally, if you want to use exponents, you'd use two asterisks, since one is used for multiplication. So 3 squared would be 3**2. Adding an exponent to the equation, we have echo 5**2, that's 5 squared, times 6 + 3 in parentheses, minus one. This will print out 224. These operations can be super powerful but sometimes unpredictable. So using echo statements to test and make sure you're getting the expected results is a must.
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.