-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path04_conditionals.php
77 lines (63 loc) · 1.39 KB
/
04_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
<?php
/* ---- Conditionals & Operators ---- */
/* ------------ Operators ----------- */
/*
< Less than
> Greater than
<= Less than or equal to
>= Greater than or equal to
== Equal to
=== Identical to
!= Not equal to
!== Not identical to
*/
/* ---------- If & If-Else Statements --------- */
/*
** If Statement Syntax
if (condition) {
// code to be executed if condition is true
}
*/
$age = 20;
// if ($age >= 18) {
// echo "You are old enough to vote";
// } else {
// echo "You are not old enough to vote";
// }
$t = date("H");
// if ($t < 12) {
// echo "Good morning!";
// } elseif ($t < 17) {
// echo "Good afternoon!";
// } else {
// echo "Good evening!";
// }
$posts = ["First post"];
// if (!empty($posts)) {
// echo $posts[0];
// } else {
// echo "No posts found";
// }
/* -------- Ternary Operator -------- */
/*
The ternary operator is a shorthand if statement.
Ternary Syntax:
condition ? true : false;
*/
// echo !empty($posts) ? $posts[0] : "No posts found";
// $firstPost = $posts[0] ?? "null";
/* -------- Switch Statements ------- */
$favColor = "red";
switch ($favColor) {
case "red":
echo "Your favorite color is red!";
break;
case "blue":
echo "Your favorite color is blue!";
break;
case "green":
echo "Your favorite color is green!";
break;
default:
echo "Your favorite color is neither red, blue, nor green!";
}