-
Notifications
You must be signed in to change notification settings - Fork 0
/
06_conditionals.php
83 lines (74 loc) · 1.9 KB
/
06_conditionals.php
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
$age = 20;
$salary = 300000;
// if condition
echo "<h3>if condition</h3>";
if ($age > 25) {
echo "You are not young" . "<br>";
}
if ($age < 25) {
echo "You are young" . "<br>";
}
// if condition - else
echo "<h3>if condition - else</h3>";
if ($age > 25) {
echo "You are not young" . "<br>";
} else {
echo "You are young" . "<br>";
}
// if condition1 AND condition2
if ($age < 22 && $salary < 500000) {
echo "You are young and not rich" . "<br>";
}
// if condition1 OR condition2
if ($age < 22 || $salary > 5000000) {
echo "You are young or you are rich" . "<br>";
}
// if condition1 - elseif condition2 - else
if ($age < 22) {
echo "Young" . "<br>";
} elseif ($age < 30) {
echo "You are not young but not old" . "<br>";
} elseif ($age < 60) {
echo "You are old but not too old" . "<br>";
} else {
echo "You are too old" . "<br>";
}
// if condition1 and condition2 - elseif condition1 and condition2 - else
if ($age < 22 && $salary >= 500000) {
echo "You are young man AND rich!!" . "<br>";
} elseif ($age < 22 && $salary < 500000) {
echo "You are young, and not so rich" . "<br>";
} elseif ($age < 60 && $salary >= 500000) {
echo "You are old, but rich" . "<br>";
} elseif ($age > 60 && $salary < 500000) {
echo "You are old and NOT rich also" . "<br>";
} else {
echo "You are in last stage";
}
// Ternary if
echo $age < 22 ? "Young" : "Not young";
// Null coalescing operator
$myAge = $age ? $age : 18;
echo $myAge;
// Null coalescing assignment operator. Since PHP 7.4
$person = [
"name" => "John"
];
if (!isset($person["name"])) {
$person["name"] = "Anonumous";
}
$person["name"] ??= "Anonymous";
echo $person["name"] . "<br>";
// switch
$userRole = "editor";
switch ($userRole) {
case "admin":
echo "You are admin";
break;
case "editor":
echo "You can edit articles";
break;
default:
echo "Unknown role";
}