PHP: Operators
In math and programming, we often use operation signs such as +, -, *, and others. In programming, such signs are called operators.
- An operator is a symbol or word that denotes an action.
- Operands are the values to which the operator is applied.
Example:
<?php
print_r(8 + 2);Here:
+is an operator8and2are operands- the result is
10
operand operator operand result
8 + 2 → 10
5 - 3 → 2
4 * 3 → 12Operations that require two operands are called binary. In the case of addition, we have two operands: one to the left of the + sign and one to the right. If at least one operand is omitted, for example, 3 + ;, the program will end with a syntax error.
Unary operators
There are also unary operations that work with a single operand. Example:
<?php
print_r(-3); // => -3In this case, - is a unary operator, and 3 is an operand. The interpreter receives the command: "take the number 3 and change its sign".
The - operator can be used in different ways. When it stands between two numbers, it is a subtraction operation:
<?php
print_r(5 - 2); // => 3
print_r(10 - 7); // => 3Here - takes the first number and subtracts the second from it.
This difference is especially noticeable when working with negative numbers. For example:
<?php
// minus times minus gives plus
print_r(5 - -2); // => 7First, we see a subtraction operation: 5 - (...). But on the right is a unary minus -2, which turns 2 into a negative number. As a result, we get: 5 - (-2) = 7.
Thus, the meaning of - depends on the context: if there is another number next to it, it is subtraction; otherwise, it is changing the sign of a number.
The same applies to the plus:
<?php
print_r(+3); // => 3
print_r(1 + +3); // => 4The main thing to remember here is that the behavior and even the notation itself fully correspond to how we did this in school.
Errors in calculations and parsing
If you treat -3 as a single number, you might not notice that - is a separate operator with its own precedence. For example:
<?php
print_r(-3 ** 2);At first glance, it might seem that -3 is being squared, and the result should be 9 (any number squared becomes positive). But the result will be -9.
The reason is the order of calculations: first, exponentiation (**) is performed, and only then the unary minus is applied. That is, the program computes it like this: -(3 ** 2) = -9. We will talk about operator precedence in more detail later in the course.
Instructions
A submarine is at a depth of -81 m. The deck of a rescue ship is located at a height of +6 m above the water level.
Calculate and print to the screen:
- The total ascent distance in meters.
- The ascent time in minutes — the submarine rises at a speed of 3 m/min.
Note that the results will be printed "glued" together in one line without a space. We will learn how to solve this problem in future lessons.
Tips
Always separate arithmetic operators from their operands with spaces – this is good programming style.
If you've reached a deadlock it's time to ask your question in the «Discussions». How ask a question correctly:
- Be sure to attach the test output, without it it's almost impossible to figure out what went wrong, even if you show your code. It's complicated for developers to execute code in their heads, but having a mistake before their eyes most probably will be helpful.
Your exercise will be checked with these tests:
<?php
namespace HexletBasics\Arithmetics\Operator;
use HexletBasics\Exercise\TestCase;
class SolutionTest extends TestCase
{
public function test()
{
$expected = '8729';
$this->assertOutput($expected);
}
}Teacher's solution will be available in:
20:00
