-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path06_conditionals.php
127 lines (106 loc) · 2.98 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
$age = 15;
$salary = 300000;
echo "------------------------------------------";
echo " <br>";
echo 'if condition' . '<br>';
echo "------------------------------------------";
echo " <br>";
if ( $age === 25 ) {
echo "you are 25" . '<br>';
}
if ( $age > 25 ) {
echo "you are not young" . '<br>';
}
if ( $age < 25 ) {
echo "you are young" . '<br>';
}
echo "------------------------------------------";
echo " <br>";
echo 'if condition - else' . '<br>';
echo "------------------------------------------";
echo " <br>";
if ( $age >= 25 ) {
echo "you are not young" . '<br>';
} else {
echo "you are young" . '<br>';
}
echo "------------------------------------------";
echo " <br>";
echo 'if condition 1 - AND condition 2' . '<br>';
echo "------------------------------------------";
echo " <br>";
if ( $age < 22 && $salary < 50000 ) {
echo "you are young and not too rich" . "<br>";
} else {
echo "you are young and too rich" . "<br>";
}
echo "------------------------------------------";
echo " <br>";
echo 'if condition 1 - OR condition 2' . '<br>';
echo "------------------------------------------";
echo " <br>";
if ( $age > 22 || $salary < 50000 ) {
echo "you are young and not too rich" . "<br>";
} else {
echo "you are young and too rich" . "<br>";
}
echo "------------------------------------------";
echo " <br>";
echo 'if condition 1 - elseif condition 2 - else' . '<br>';
echo "------------------------------------------";
echo " <br>";
if ( $age < 22 ) {
echo "you are not young but not old" . '<br>';
} elseif ( $age > 30 ) {
echo "you are old ut not too old" . '<br>';
} else {
echo "you are too old" . '<br>';
}
echo "------------------------------------------";
echo " <br>";
echo 'Ternary if de' . '<br>';
echo "------------------------------------------";
echo " <br>";
echo $age > 22 ? 'Young' :
( $age < 30 ? 'not young but not old' . '<br>' :
'old' . '<br>' );
echo "------------------------------------------";
echo " <br>";
echo 'Null coalescing operator' . '<br>';
echo "------------------------------------------";
echo " <br>";
//$myAge = $age ? $age : 18;
$myAge = $age ? : 18;
echo $myAge. '<br>';
//$myAdress = isset($adress)?$adress: 'current location'. '<br>';
$myAdress =$adress?? 'current location'. '<br>';
echo $myAdress;
echo "------------------------------------------";
echo " <br>";
echo 'Null coalescing assigment operator' . '<br>';
echo "------------------------------------------";
echo " <br>";
$person=[
'name'=>'Usher'
];
//check if person name exist
$person['name'] ??='Anonymous'; //if person name exist output
// it else output Anonymous
echo $person['name'] . '<br>';
echo "------------------------------------------";
echo " <br>";
echo 'SWITCH' . '<br>';
echo "------------------------------------------";
echo " <br>";
$userRole = 'admin';
switch ($userRole){
case 'admin':
echo "You are the admin" . "<br>";
break;
case 'editor':
echo "You can edit article" . "<br>";
break;
default:
echo "Unknown role" . "<br>";
}