Building Strings with Dots and Variables
Lab: PHP 101: Gentle Introduction to WordPress Programming
Video Runtime: 15:10
PHP gives you the means to build dynamic strings. You can run a function and then append a string literal to the end of it. You can embed a variable into a string. You can append the value of the variable to another string. You see it in the themes and plugins, as well as WordPress Core. Let’s talk about it.
With the PHP concatenation operator, i.e. .
(dot), you are able to smoosh two string values together to make a new string. For example, look at this code:
include_once( get_template_directory() . '/lib/init.php' );
What is the above code doing? It’s loading a file using include_once()
. The file’s full path on the hard drive is dynamically built at runtime. The root to the theme’s directory (this is the parent theme) is fetched using the WordPress function get_template_directory()
. Then using the dot the file’s path and filename within the theme is appended to the end of the first part. That new string is passed into the include, which then locates and loads the file into memory.
Look at your theme. Find one of the file loaders. You’ll see this technique being used. It’s prevalent in WordPress.
In this episode, you’ll use the following code to test and see how this works. I’ll walk you through it step-by-step and see the values as well as the sequence of how the string is put together.
-
Need the code used in this video? Click here to get it
Then you’ll learn about embedding variables within a string and its syntax. PHP lets you embed a variable with the following syntax:
- The string must be wrapped in a double quote (not single).
- Place the variable where you want it to appear within the single.
- Wrap the variable in curly braces
For example,
$suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min'; d( get_stylesheet_directory_uri() . "/js/responsive-menus{$suffix}.js" );
Look at the syntax of the string on the second line. It starts with a double quote, then has a string literal, then an opening curly brace, then the variable, closed by the curly brace, etc. When this code runs, the value bound to that variable will be inserted into the string.
Want to learn more about the ternary operator? Click here to watch this quick tip.
Challenge Yourself
$file = '/js/responsive-menus{$suffix}.js';
-
For the above code, if the suffix is an empty string, what is the value of $file when this code runs? Why?
$theme_directory = get_stylesheet_directory(); $file_in_theme = '/lib/theme-defaults.php'; include( $theme_directory . $file_in_theme ); include( get_stylesheet_directory() . '/lib/theme-defaults.php' );
-
True or False. Using the above code, the theme-defaults.php file is only loaded once.
$current_user = wp_get_current_user(); $first_name = $current_user->user_firstname; $message = "Hello and Welcome, {$first_name}";
-
For the above code, what is the value of the message if the user is Sally Jones?
Break. Go rest your noodle for a couple of minutes.
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