-
Notifications
You must be signed in to change notification settings - Fork 525
/
04_conditionals.php
97 lines (75 loc) · 1.99 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?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 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 'Sorry, you are too young to vote.';
}
// Dates
// $today = date("F j, Y, g:i a");
$t = date('H');
if ($t < 12) {
echo 'Have a good morning!';
} elseif ($t < 17) {
echo 'Have a good afternoon!';
} else {
echo 'Have a good evening!';
}
// Check if an array is empty
// The isset() function will generate a warning or e-notice when the variable does not exists. The empty() function will not generate any warning or e-notice when the variable does not exists.
$posts = [];
if (!empty($posts[0])) {
echo $posts[0];
} else {
echo 'There are no posts';
}
/* -------- Ternary Operator -------- */
/*
The ternary operator is a shorthand if statement.
Ternary Syntax:
condition ? true : false;
*/
// Echo based on a condition (Same as above)
echo !empty($posts[0]) ? $posts[0] : 'There are no posts';
// Assign a variable based on a condition
$firstPost = !empty($posts[0]) ? $posts[0] : 'There are no posts';
$firstPost = !empty($posts[0]) ? $posts[0] : null;
// Null Coalescing Operator ?? (PHP 7.4)
// Will return null if $posts is empty
// Always returns first parameter, unless first parameter happens to be NULL
$firstPost = $posts[0] ?? null;
var_dump($firstPost);
/* -------- 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 not red, blue, nor green!';
}