Appearance
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
php
<article>
<?php
$x = 10;
$y = 3;
echo "<p> \$x = $x </p>\n";
echo "<p> \$y = $y </p>\n";
echo "<hr>\n";
echo "<pre><code> \$x + \$y = $x + $y = " . ($x + $y) . "</code></pre>\n";
echo "<pre><code> \$x - \$y = $x - $y = " . ($x - $y) . "</code></pre>\n";
echo "<pre><code> \$x * \$y = $x * $y = " . ($x * $y) . "</code></pre>\n";
echo "<pre><code> \$x / \$y = $x / $y = " . ($x / $y) . "</code></pre>\n";
echo "<pre><code> \$x % \$y = $x % $y = " . ($x % $y) . "</code></pre>\n";
echo "<pre><code> \$x ** \$y = $x ** $y = " . ($x ** $y) . "</code></pre>\n";
?>
</article>
<article>
<?php
$x = 10;
$y = 3;
echo "<p> \$x = $x </p>\n";
echo "<p> \$y = $y </p>\n";
echo "<hr>\n";
echo "<pre><code> \$x + \$y = $x + $y = " . ($x + $y) . "</code></pre>\n";
echo "<pre><code> \$x - \$y = $x - $y = " . ($x - $y) . "</code></pre>\n";
echo "<pre><code> \$x * \$y = $x * $y = " . ($x * $y) . "</code></pre>\n";
echo "<pre><code> \$x / \$y = $x / $y = " . ($x / $y) . "</code></pre>\n";
echo "<pre><code> \$x % \$y = $x % $y = " . ($x % $y) . "</code></pre>\n";
echo "<pre><code> \$x ** \$y = $x ** $y = " . ($x ** $y) . "</code></pre>\n";
?>
</article>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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>\n";
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 "<pre><code> \$x + \$y = $x + $y = " . ($x + $y) . "</code></pre>\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 "<pre><code> \$x + \$y = $x + $y = $sum</code></pre>\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
php
<article>
<?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";
echo "<hr>\n";
echo "<pre><code> \$x += \$y; // \$x = \$x + \$y = $x + $y </code></pre>\n";
$x += $y;
echo "<p> Result: <b>\$x = $x</b></p>\n<hr>\n";
echo "<pre><code> \$x -= \$z; // \$x = \$x - \$z = $x - $z </code></pre>\n";
$x -= $z;
echo "<p> Result: <b>\$x = $x </b></p>\n<hr>\n";
echo "<pre><code> \$x *= \$z; // \$x = \$x * \$z = $x * $z </code></pre>\n";
$x *= $z;
echo "<p> Result: <b>\$x = $x </b></p>\n<hr>\n";
echo "<pre><code> \$x /= \$z; // \$x = \$x / \$z = $x / $z </code></pre>\n";
$x /= $z;
echo "<p> Result: <b>\$x = $x </b></p>\n<hr>\n";
echo "<pre><code> \$x %= \$y; // \$x = \$x % \$y = $x % $y </code></pre>\n";
$x %= $y;
echo "<p> Result: <b>\$x = $x</b></p>\n<hr>\n";
echo "<pre><code> \$y++; // \$y = \$y + 1 = $y + 1 </code></pre>\n";
$y++;
echo "<p> Result: <b>\$y = $y </b></p>\n<hr>\n";
echo "<pre><code> \$z--; // \$z--; </code></pre>\n";
$z--;
echo "<p> Result: <b>\$z = $z </b></p>\n";
?>
</article>
<article>
<?php
$name = 'John';
$surname = 'Doe';
echo "<p> \$name = '$name' </p>\n";
echo "<p> \$surname = '$surname' </p>\n";
echo "<hr>\n";
echo "<pre><code> \$name .= \$surname; // \$name = \$name . \$surname = '$name' . '$surname' </code></pre>\n";
$name .= $surname;
echo "<p> Result: <b>\$name = '$name' </b></p>\n";
?>
</article>
<article>
<?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";
echo "<hr>\n";
echo "<pre><code> \$x += \$y; // \$x = \$x + \$y = $x + $y </code></pre>\n";
$x += $y;
echo "<p> Result: <b>\$x = $x</b></p>\n<hr>\n";
echo "<pre><code> \$x -= \$z; // \$x = \$x - \$z = $x - $z </code></pre>\n";
$x -= $z;
echo "<p> Result: <b>\$x = $x </b></p>\n<hr>\n";
echo "<pre><code> \$x *= \$z; // \$x = \$x * \$z = $x * $z </code></pre>\n";
$x *= $z;
echo "<p> Result: <b>\$x = $x </b></p>\n<hr>\n";
echo "<pre><code> \$x /= \$z; // \$x = \$x / \$z = $x / $z </code></pre>\n";
$x /= $z;
echo "<p> Result: <b>\$x = $x </b></p>\n<hr>\n";
echo "<pre><code> \$x %= \$y; // \$x = \$x % \$y = $x % $y </code></pre>\n";
$x %= $y;
echo "<p> Result: <b>\$x = $x</b></p>\n<hr>\n";
echo "<pre><code> \$y++; // \$y = \$y + 1 = $y + 1 </code></pre>\n";
$y++;
echo "<p> Result: <b>\$y = $y </b></p>\n<hr>\n";
echo "<pre><code> \$z--; // \$z--; </code></pre>\n";
$z--;
echo "<p> Result: <b>\$z = $z </b></p>\n";
?>
</article>
<article>
<?php
$name = 'John';
$surname = 'Doe';
echo "<p> \$name = '$name' </p>\n";
echo "<p> \$surname = '$surname' </p>\n";
echo "<hr>\n";
echo "<pre><code> \$name .= \$surname; // \$name = \$name . \$surname = '$name' . '$surname' </code></pre>\n";
$name .= $surname;
echo "<p> Result: <b>\$name = '$name' </b></p>\n";
?>
</article>
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
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
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
php
<article>
<?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";
echo "<hr>\n";
echo "<pre><code> (\$x1 == \$x2) = ($x1 == '$x2') = " . ($x1 == $x2) . "</code></pre>\n";
echo "<pre><code> (\$x1 === \$x2) = ($x1 === '$x2') = " . ($x1 === $x2) . "</code></pre>\n";
echo "<pre><code> (\$x1 != \$y) = ($x1 != $y) = " . ($x1 != $y) . "</code></pre>\n";
echo "<pre><code> (\$x2 <> \$y) = ('$x2' <> $y) = " . ($x2 <> $y) . "</code></pre>\n";
echo "<pre><code> (\$x1 < \$y) = ($x1 < $y) = " . ($x1 < $y) . "</code></pre>\n";
echo "<pre><code> (\$x1 > \$y) = ($x1 > $y) = " . ($x1 > $y) . "</code></pre>\n";
echo "<pre><code> (\$x2 > \$y) = ('$x2' > $y) = " . ($x2 > $y) . "</code></pre>\n";
?>
</article>
<article>
<?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";
echo "<hr>\n";
echo "<pre><code> (\$x1 == \$x2) = ($x1 == '$x2') = " . ($x1 == $x2) . "</code></pre>\n";
echo "<pre><code> (\$x1 === \$x2) = ($x1 === '$x2') = " . ($x1 === $x2) . "</code></pre>\n";
echo "<pre><code> (\$x1 != \$y) = ($x1 != $y) = " . ($x1 != $y) . "</code></pre>\n";
echo "<pre><code> (\$x2 <> \$y) = ('$x2' <> $y) = " . ($x2 <> $y) . "</code></pre>\n";
echo "<pre><code> (\$x1 < \$y) = ($x1 < $y) = " . ($x1 < $y) . "</code></pre>\n";
echo "<pre><code> (\$x1 > \$y) = ($x1 > $y) = " . ($x1 > $y) . "</code></pre>\n";
echo "<pre><code> (\$x2 > \$y) = ('$x2' > $y) = " . ($x2 > $y) . "</code></pre>\n";
?>
</article>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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 ` | ` | |
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
php
<article>
<?php
$x = 6;
$y = 4;
echo "<p> \$x = $x </p>\n";
echo "<p> \$y = $y </p>\n";
echo "<hr>\n";
echo "<pre><code> (\$x < 8 and \$y > 3) = ($x < 8 and $y > 3) = " . ($x < 8 and $y > 3) . "</code></pre>\n";
echo "<pre><code> (\$x < 8 && \$y > 6) = ($x < 8 && $y > 6) = " . ($x < 8 && $y > 6) . "</code></pre>\n";
echo "<pre><code> (\$x < 8 or \$y > 3) = ($x < 8 or $y > 3) = " . ($x < 8 or $y > 3) . "</code></pre>\n";
echo "<pre><code> (\$x < 8 || \$y > 6) = ($x < 8 || $y > 6) = " . ($x < 8 || $y > 6) . "</code></pre>\n";
echo "<pre><code> (\$x < 8 xor \$y > 3) = ($x < 8 xor $y > 3) = " . ($x < 8 xor $y > 3) . "</code></pre>\n";
echo "<pre><code> (\$x < 8 xor \$y > 6) = ($x < 8 xor $y > 6) = " . ($x < 8 xor $y > 6) . "</code></pre>\n";
echo "<pre><code> !(\$x == \$y) = !($x == $y) = " . !($x == $y) . "</code></pre>\n";
?>
</article>
<article>
<?php
$x = 6;
$y = 4;
echo "<p> \$x = $x </p>\n";
echo "<p> \$y = $y </p>\n";
echo "<hr>\n";
echo "<pre><code> (\$x < 8 and \$y > 3) = ($x < 8 and $y > 3) = " . ($x < 8 and $y > 3) . "</code></pre>\n";
echo "<pre><code> (\$x < 8 && \$y > 6) = ($x < 8 && $y > 6) = " . ($x < 8 && $y > 6) . "</code></pre>\n";
echo "<pre><code> (\$x < 8 or \$y > 3) = ($x < 8 or $y > 3) = " . ($x < 8 or $y > 3) . "</code></pre>\n";
echo "<pre><code> (\$x < 8 || \$y > 6) = ($x < 8 || $y > 6) = " . ($x < 8 || $y > 6) . "</code></pre>\n";
echo "<pre><code> (\$x < 8 xor \$y > 3) = ($x < 8 xor $y > 3) = " . ($x < 8 xor $y > 3) . "</code></pre>\n";
echo "<pre><code> (\$x < 8 xor \$y > 6) = ($x < 8 xor $y > 6) = " . ($x < 8 xor $y > 6) . "</code></pre>\n";
echo "<pre><code> !(\$x == \$y) = !($x == $y) = " . !($x == $y) . "</code></pre>\n";
?>
</article>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16