-
Notifications
You must be signed in to change notification settings - Fork 53
/
06_conditionals.php
86 lines (79 loc) · 2.16 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
84
85
86
<?php
$age = 20;
$salary = 300000;
//// 1. if condition
//if ($age < 22) { // 1.1 Without curly braces { }
// echo 'You are young man!!<br>';
//}
//
//// 2. if condition - else
//if ($age < 22) {
// echo 'You are young man!!<br>';
//} else {
// echo 'You are not young man!!<br>';
//}
//
//// 3. if condition1 AND condition2
//if ($age < 22 && $salary > 500000) {
// echo 'You are young AND rich<br>';
//}
//
//// 4. if condition1 OR condition2
//if ($age < 22 || $salary > 500000) {
// echo 'You are young OR rich<br>';
//}
//
//// 5. if condition1 - elseif condition2 - else
//if ($age < 22) {
// echo 'You are young man!!<br>';
//} elseif ($age > 60) {
// echo 'You are old<br>';
//} else {
// echo "You are not young, but not old also<br>";
//}
//// 6. 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>';
//}
// 7. Ternary if
echo $age < 22 ? 'Young' : 'Old';
echo '<br>';
// 7.1 Nested ternary
echo $age < 22 ? ($age < 16 ? 'Too young' : 'Young') : 'Old';
echo '<br>';
// 7.2 Short ternary
$myAge = $age ?: 18; // Equivalent of "$age ? $age : 18"
// 8. Null coalescing operator
$var = isset($name) ? $name : 'John';
$var = $name ?? 'John'; // Equivalent of above
echo $var.'<br>';
// 9. Null coalescing assignment operator. Since PHP 7.4
$person = [
'name' => 'John' // 9.1 Comment this line
];
if (!isset($person['name'])){
$person['name'] = 'Anonymous';
}
$person['name'] ??= 'Anonymous';
echo $person['name'].'<br>';
// 10. switch
$userRole = 'admin'; // admin, editor, user
switch ($userRole) {
case 'admin':
echo 'You can do anything<br>';
break;
case 'editor';
echo 'You can edit content<br>';
break;
case 'user':
echo 'You can view posts and comment<br>';
break;
default:
echo 'Unknown role<br>';
}