PHP: Operators
Let's learn some important terms before moving forwards. An operation sign like +
is called an operator. It's just a symbol that produces a certain operation. Later you'll learn that there are other operators in PHP, than just arithmetic ones.
<?php
print_r(8 + 2);
In this example +
is an operator, and numbers 8
and 2
are operands.
In case of addition there're two operands: one on the left, one on the right from +
. Operators that require two operands are called binary. If you skip at least one operand, like so 3 + ;
, the program will exit with an error.
There are also unary operations (with one operand) and even ternary (with three operands)!
Symbols + and - are used not only as operators. As far as negative numbers are concerned, the minus sign becomes part of the number:
<?php
print_r(-3); // => -3
// The same as 4 - 3
print_r(4 + -3); // => 1
The plus sign can work in the same way:
<?php
print_r(+3); // => 3
print_r(1 + +3); // => 4
Instructions
Write a program that calculates the difference between 6
and -81
, and prints the result to screen.
Definitions
Arithmetic operator - addition, subtraction, multiplication, division.
Operator - special symbol that creates an action. For example,
+
creates addition.Operand - that what is being used with an operator.
3 * 6
: here 3 and 6 are operands.Unary operation - operation that requires one operand.
Binary operation - operation that requires two operands.