Operators
Arithmetic operators
operator | description | example | result |
---|---|---|---|
+ | addition | $x + $y | sum of $x and $y |
- | subtraction | $x - $y | difference of $x and $y |
* | multiplication | $x * $y | product of $x and $y |
/ | division | $x / $y | quotient of $x and $y |
% | modulus | $x % $y | remainder of $x divided by $y |
** | power | $x ** $y | $x to the power $y |
- Open course/arithmetic_operators.php
<div class="margin-3">
<?php
$x = 10; // Assignment operator
$y = 3; // Assignment operator
echo "<p> \$x = $x </p>\n";
echo "<p> \$y = $y </p>\n";
?>
</div>
<div class="margin-3">
<?php
echo "<p> \$x + \$y = $x + $y = " . ($x + $y) . "</p>\n";
echo "<p> \$x - \$y = $x - $y = " . ($x - $y) . "</p>\n";
echo "<p> \$x * \$y = $x * $y = " . ($x * $y) . "</p>\n";
echo "<p> \$x / \$y = $x / $y = " . ($x / $y) . "</p>\n";
echo "<p> \$x % \$y = $x % $y = " . ($x % $y) . "</p>\n";
echo "<p> \$x ** \$y = $x ** $y = " . ($x ** $y) . "</p>\n";
?>
</div>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
REMARKS
- If you escape the dollar sign in a double-quoted (or heredoc) string, the corresponding variable won't be interpolated:
echo "<p> \$x = $x </p>";
results in$x = 10
as output - You can't (easily) include arithmetic operations in a double quoted string. Therefore, we resort to string concatenation in the statement
echo "<p> \$x + \$y = $x + $y = " . ($x + $y) . "</p>\n";
. You should also enclose the calculation($x + $y)
with round brackets to get an error-free statement.- As an alternative, you could use an additional variable
$sum = $x + $y
, after which you can writeecho "<p> \$x + \$y = $x + $y = $sum </p>\n";
- As an alternative, you could use an additional variable
Assignment operators
operator | description | example | result |
---|---|---|---|
= | assign | $x = $y | |
+= | add and assign | $x += $y | $x = $x + $y |
-= | subtract and assign | $x -= $y | $x = $x - $y |
*= | multiply and assign | $x *= $y | $x = $x * $y |
/= | divide and assign quotient | $x /= $y | $x = $x / $y |
%= | divide and assign modulus | $x %= $y | $x = $x % $y |
.= | concatenate and assign | $x .= $y | $x = $x . $y |
++ | increment and assign | $x++ | $x = $x + 1 |
-- | decrement and assign | $x-- | $x = $x - 1 |
- Open course/assignment_operators.php
<div class="margin-3">
<?php
$x = 10;
$y = 2;
$z = 3;
echo "<p> \$x = $x </p>\n";
echo "<p> \$y = $y </p>\n";
echo "<p> \$z = $z </p>\n";
?>
</div>
<div class="margin-3">
<?php
echo "<blockquote>Execute the statement <code>\$x += \$y; <span class='text-lightgray'>// \$x = \$x + \$y = $x + $y</span></code></blockquote>\n";
$x += $y;
echo "<p> \$x = $x </p>\n";
echo "<blockquote>Execute the statement <code>\$x -= \$z; <span class='text-lightgray'>// \$x = \$x - \$z = $x - $z</span></code></blockquote>\n";
$x -= $z;
echo "<p> \$x = $x </p>\n";
echo "<blockquote>Execute the statement <code>\$x *= \$z; <span class='text-lightgray'>// \$x = \$x * \$z = $x * $z</span></code></blockquote>\n";
$x *= $z;
echo "<p> \$x = $x </p>\n";
echo "<blockquote>Execute the statement <code>\$x /= \$z; <span class='text-lightgray'>// \$x = \$x / \$z = $x / $z</span></code></blockquote>\n";
$x /= $z;
echo "<p> \$x = $x </p>\n";
echo "<blockquote>Execute the statement <code>\$x %= \$y; <span class='text-lightgray'>// \$x = \$x % \$y = $x % $y</span></code></blockquote>\n";
$x %= $y;
echo "<p> \$x = $x </p>\n";
echo "<blockquote>Execute the statement <code>\$y++; <span class='text-lightgray'>// \$y = \$y + 1 = $y + 1</span></code></blockquote>\n";
$y++;
echo "<p> \$y = $y </p>\n";
echo "<blockquote>Execute the statement <code>\$z--; <span class='text-lightgray'>// \$z = \$z - 1 = $z - 1</span></code></blockquote>\n";
$z--;
echo "<p> \$z = $z </p>\n";
?>
</div>
<hr>
<div class="margin-3">
<?php
$name = 'John';
$surname = 'Doe';
echo "<p> \$name = '$name' </p>\n";
echo "<p> \$surname = '$surname' </p>\n";
?>
</div>
<div class="margin-3">
<?php
echo "<blockquote>Execute the statement <code>\$name .= \$surname; <span class='text-lightgray'>// \$name = \$name . \$surname = '$name' . '$surname'</span></code></blockquote>\n";
$name .= $surname;
echo "<p> \$name = '$name' </p>\n";
?>
</div>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
Comparison operators
operator | description | example | result |
---|---|---|---|
== | equal | $x == $y | true if $x is equal to $y |
=== | identical | $x === $y | true if $x is equal to $y and they are of the same type |
!= <> | not equal | $x != $y $x <> $y | true if $x is not equal to $y |
!== | not identical | $x !== $y | true if $x is not equal to $y or they are not of the same type |
< | less than | $x < $y | true if $x is less than $y |
<= | less than or equal to | $x <= $y | true if $x is less than or equal to $y |
> | greater than | $x > $y | true if $x is greater than $y |
>= | greater than or equal to | $x >= $y | true if $x is greater than or equal to $y |
- Open course/comparison_operators.php
<div class="margin-3">
<?php
$x1 = 10;
$x2 = '10';
$y = 5;
echo "<p> \$x1 = $x1 </p>\n";
echo "<p> \$x2 = '$x2' </p>\n";
echo "<p> \$y = $y </p>\n";
?>
</div>
<div class="margin-3">
<?php
echo "<p> (\$x1 == \$x2) = ($x1 == '$x2') = " . ($x1 == $x2) . "</p>\n";
echo "<p> (\$x1 === \$x2) = ($x1 === '$x2') = " . ($x1 === $x2) . "</p>\n";
echo "<p> (\$x1 != \$y) = ($x1 != $y) = " . ($x1 != $y) . "</p>\n";
echo "<p> (\$x2 <> \$y) = ('$x2' <> $y) = " . ($x2 <> $y) . "</p>\n";
echo "<p> (\$x1 < \$y) = ($x1 < $y) = " . ($x1 < $y) . "</p>\n";
echo "<p> (\$x1 > \$y) = ($x1 > $y) = " . ($x1 > $y) . "</p>\n";
echo "<p> (\$x2 > \$y) = ('$x2' > $y) = " . ($x2 > $y) . "</p>\n";
?>
</div>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
REMARK
Notice that true
is depicted as 1
, while false
corresponds to an empty string
Logical operators
operator | example | result |
---|---|---|
and && | $x and $y $x && $y | true if both $x and $y are true |
or || | $x or $y $x || $y | true if either $x or $y is true |
xor | $x xor $y | true if either $x or $y is true, but not both |
! | !$x | true if $x is false (not true ) |
- Open course/logical_operators.php
<div class="margin-3">
<?php
$x = 6;
$y = 4;
echo "<p> \$x = $x </p>\n";
echo "<p> \$y = $y </p>\n";
?>
</div>
<div class="margin-3">
<?php
echo "<p> (\$x < 8 and \$y > 3) = ($x < 8 and $y > 3) = " . ($x < 8 and $y > 3) . "</p>\n";
echo "<p> (\$x < 8 && \$y > 6) = ($x < 8 && $y > 6) = " . ($x < 8 && $y > 6) . "</p>\n";
echo "<p> (\$x < 8 or \$y > 3) = ($x < 8 or $y > 3) = " . ($x < 8 or $y > 3) . "</p>\n";
echo "<p> (\$x < 8 || \$y > 6) = ($x < 8 || $y > 6) = " . ($x < 8 || $y > 6) . "</p>\n";
echo "<p> (\$x < 8 xor \$y > 3) = ($x < 8 xor $y > 3) = " . ($x < 8 xor $y > 3) . "</p>\n";
echo "<p> (\$x < 8 xor \$y > 6) = ($x < 8 xor $y > 6) = " . ($x < 8 xor $y > 6) . "</p>\n";
echo "<p> !(\$x == \$y) = !($x == $y) = " . !($x == $y) . "</p>\n";
?>
</div>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20