forked from Kiotolabs/structured-programming-lab3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlab3_task1.php
More file actions
123 lines (97 loc) · 3.46 KB
/
lab3_task1.php
File metadata and controls
123 lines (97 loc) · 3.46 KB
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
<?php
/**
* ICS 2371 — Lab 3: Control Structures I
* Task 1: Temperature Alert System [8 marks]
*
* IMPORTANT: You must complete pseudocode AND flowchart in your PDF
* report BEFORE writing any code below. Marks are awarded for all
* three components: pseudocode, flowchart, and working code.
*
* @author [Kevin Mutua]
* @student [ENE212-0078/2023]
* @lab Lab 3 of 14
* @unit ICS 2371
* @date [1 April 2026]
*/
// ══════════════════════════════════════════════════════════════
// EXERCISE A — Temperature Alert System
// ══════════════════════════════════════════════════════════════
// Change value to test 36.8, 39.2, and 34.5
$temperature = 39.2;
// 1. Check for Normal range (inclusive: 36.1 to 37.5)
if ($temperature >= 36.1 && $temperature <= 37.5) {
echo "Normal";
}
// 2. Check for Fever
if ($temperature > 37.5) {
echo "Fever";
}
// 3. Check for Hypothermia Warning
if ($temperature < 36.1) {
echo "Hypothermia Warning";
}
// ══════════════════════════════════════════════════════════════
// EXERCISE B — Even or Odd
// ══════════════════════════════════════════════════════════════
$number = 47;
// 1. Even or Odd Check
if ($number % 2 == 0) {
echo "$number is EVEN<br>";
} else {
echo "$number is ODD<br>";
}
// 2. Divisibility by 3
if ($number % 3 == 0) {
echo "$number is divisible by 3<br>";
}else {
echo "$number is not divisible by 3<br>";
}
// 3. Divisibility by 5
if ($number % 5 == 0) {
echo "$number is divisible by 5<br>";
}else {
echo "$number is not divisible by 5<br>";
}
// 4. Divisibility by both
if ($number % 3 == 0 && $number % 5 == 0) {
echo "$number is divisible by both 3 and 5<br>";
}else {
echo "$number is not divisible by both<br>";
}
// ==========================================
// Exercise C — Comparison Chain
// ==========================================
$x = 10; // Integer
$y = "10"; // String
$z = 10.0; // Float (Decimal)
echo "<h3>Exercise C Results:</h3>";
echo "<pre>"; // <pre> makes var_dump output easier to read in a browser
var_dump($x == $y); // A
var_dump($x === $y); // B
var_dump($x == $z); // C
var_dump($x === $z); // D
var_dump($y === $z); // E
var_dump($x <=> $y); // F
echo "</pre>";
// ==========================================
// Exercise D — Null & Default Values
// ==========================================
echo "<h3>Exercise D Results:</h3>";
// Part 1: Basic Null Coalescing
$username = null;
$display = $username ?? "Guest";
echo "Welcome, $display<br>";
// Part 2: Chained Null Coalescing
$config_val = null;
$env_val = null;
$default = "system_default";
$result = $config_val ?? $env_val ?? $default;
echo "Config: $result<br>";
// Part 3: My Custom Chained Example
// This checks for a user-set theme, then a session theme, then defaults to 'Light'
$user_theme = null;
$session_theme = "Dark Mode";
$fallback_theme = "Light Mode";
$final_theme = $user_theme ?? $session_theme ?? $fallback_theme;
echo "Selected Theme: $final_theme<br>";
?>