To Run or Not to Run – Making Decisions
Lab: PHP 101: Gentle Introduction to WordPress Programming
Video Runtime: 19:07
We need to make decisions about the state as well as information in order to determine whether to run a block of code or not. There are multiple constructs available for us to use. In this episode, we’ll focus on the if/else if/else
construct. We’ll look at different code examples in the Genesis framework and twentyseventeen themes. We’ll also talk about conditional expressions.
The code that is controlled by the decision is enclosed within the curly braces (there is alternative syntax that uses a colon for the opening curly brace and endif;
for the closing one). For that code block to run, the conditional expression must be true
.
When you have different branches to the decision with elseif
and/or else
the code within those respective code blocks runs only if the main if
conditional expression is false
and its conditional expression is true
.
With this structure, only one or none of the code blocks run. They only run if the preceding branch is false
and its conditional expression is true
.
Conditional Expressions
We will talk more about the conditional expression as well as the different operators you can use, such as an “or”, “and”, equality, greater than, not equal to, and more. There are logical operators such as ||
(which is an “or” operation) and &&
(which is an “and” operation). You also have comparison operators such as greater than, less than, equal to, etc.
We talk briefly about type juggling as we cover the difference between ==
and ===
. You can learn more about type juggling in Docx as well as in the PHP Variables Bootcamp lab.
Challenge Yourself
-
When will the code within an 'else' run?
<?php | |
$post_id = 10; | |
$image_size = 'thumbnail'; | |
if ( $image_size == 'full' || $post_id < 5 ) { | |
// run the if code block | |
} elseif ( $image_size == 'thumbnail' && $post_id == 10 ) { | |
// run the elseif code block | |
} else { | |
// do the else code block. | |
} |
-
For the above code, which code block is going to run?
-
For the following conditional expression, will it evaluate to True or False: 10 === '10'?
Additional Resources
Know the Code has plenty of content available for you to learn deeply about decisions and conditional expressions. Links are provided above. But here are some keys that I want to point out for you:
Once upon a time, there was a developer... You! This is going to be a good story...
Episodes
Total Lab Runtime: 03:21:32
- 1 Lab Introductionfree 11:37
- 2 What is PHP? Why use it?free 17:49
- 3 Why and how does WordPress use PHP?free 07:33
- 4 Syntax Basicsfree 18:34
- 5 What's the deal with Variables?free 27:05
- 6 Break Up Code into Logical Partsfree 07:55
- 7 Subroutines - Behold the functionfree 14:11
- 8 Loading Files to Runfree 13:20
- 9 To Run or Not to Run - Making Decisionsfree 19:07
- 10 Sequencing - Yup, code runs in orderfree 06:16
- 11 Repeating code using Loopsfree 13:57
- 12 Building Strings with Dots and Variablesfree 15:10
- 13 What's the deal with scope?free 08:34
- 14 Naming Stufffree 06:18
- 15 Putting it All Togetherfree 08:33
- 16 Where do I go from here?free 05:33