Skip to content
- In addition to Variables in PHP today we are going to talk about Math
- Yes, you can do calculations in PHP similar to what you do on your calculator
- Open your code editor and open a new file.
- Open an HTML, HEAD, and PHP Tags
- Between the PHP tags,
- You can do additions, subtractions, division, and multiplications
- For every function, use echo and then simply add, multiply or subtract.
- Syntax for functions – (+ for addition), (- for subtraction), (* for multiplication) and (/ for division)
- We can also do calculations with multiple functions such as “echo 45 + 34 * 45 / 421 – 45;”
- I would like to also share with you on the order of operations. Please remember that multiplication and division go before addition and subtraction.
- For example – echo 5 + 5 * 10 will show as 55 and not 100 unless you place 5+5 in prentices like (5 + 5) * 10
- Also – You can save numbers as variables and use them for calculations. For example – $number1 = 12; and $number2 = 24;. Echo $number1 + $number2;
One Reply to “How to do Math in PHP”