Subroutines – Behold the function
Lab: PHP 101: Gentle Introduction to WordPress Programming
Video Runtime: 14:11
In this episode, let’s focus on functions within PHP. PHP lets you define a user-defined function, which is a subroutine. You define it using the keyword function
. Then you give it a unique name. You can specify any parameters which are variables. These parameters allow your function to receive information to do work on when it’s called. Then you use the curly braces to block off the code that belongs to the function. That code does not run until the function is called.
Let’s look at the WordPress Core function called get_theme_mod()
. We’ll look at the twentyseventeen theme and how it calls this function. We’ll talk about the syntax as well as option parameters and how they are used.
Then you’ll write a function to see how it works with and without parameters. You’ll also set a parameter as optional and see how it works.
Challenge Yourself
-
Can you start a function name with a $ sign?
-
How do you specify a function parameter as optional?
-
True or False. When PHP loads a file, the code in a function runs immediately.
-
How do you call a function to use it in your code?
-
Your code needs to go get a theme modification function that is called 'nav_menu_locations'. How would you do it?
Hey... hey you... yes you!... Having a good time? Learning new things? Good!
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