Unlock your potential with a Pro Membership
Here is where you propel your career forward. Come join us today. Learn more.
PHP 101: Concatenating Assignment Operator
Lab: WordPress Custom Taxonomy Basics
Video Runtime: 02:26
The term “concatenating” means that we are smooshing two strings together by appending the one on the right to the one on the left. The result is a new string value.
For example, let’s say that a variable $post_meta
has a value of '[post_categories] [post_tags]'
. You want to append another shortcode to the end of it. How do you do that?
There are two different ways to achieve this and make an assignment.
Long Hand Approach
To concatenate an existing variable’s string value to some value you are processing, you have a couple of choices in PHP. You can do it with the long hand approach, like this:
$post_meta = $post_meta . ' [post_terms taxonomy="department"]';
where the shortcode’s string literal is smooshed together to the end of the string value in the variable $post_meta
. Then the new string is assigned back to the variable. That’s the long hand version.
Short Hand Approach
PHP provides you with a concatenating assignment operator as a shorthand approach:
$post_meta .= ‘ [post_terms taxonomy=”department”]’;
This code works the same as the full version; however, it’s more condensed. It eliminates the repetitious repeating of the variable. Therefore, this approach is more readable and maintainable.
This approach is very popular and prevalent! Make sure you understand it completely!
What’s the Sequence and Result?
Let’s walk through the processing and look at the result.
Step 1: Concatenate
First the variable’s value and string literal are concatenated to form a new string value of:
'[post_categories] [post_tags] [post_terms taxonomy="department"]';
Step 2: Assignment
The next step is to assign the new string to the variable $post_meta
, which means the value it represents is changed to:
$post_meta = '[post_categories] [post_tags] [post_terms taxonomy="department"]';
This Episode
In this episode, let’s talk about the process of concatenating. Then we’ll walk through how each of these approaches works. Finally, we’ll see the results.
Code Challenge
Let’s challenge you. Ready? When this function is called, a string literal of '[post_categories]'
is passed to it and assigned to the parameter $html
. What is the value returned when the function is done running?
function filter_the_entry_footer_post_meta( $html ) { | |
$html .= ' [post_terms taxonomy="department"]'; | |
return $html; | |
} | |
$content = filter_the_entry_footer_post_meta( '[post_categories]' ); | |
// what is the value of $content? |
-
Check your answer here
Who needs copy/paste? Right. Writing code from scratch is fun!
Episodes
Total Lab Runtime: 01:30:53
- 1 Lab Introductionfree 08:15
- 2 Custom Taxonomy - The What, Why, and Whenfree 08:32
- 3 Registering a Custom Taxonomypro 09:54
- 4 Configure the Labelspro 14:51
- 5 Bind to Post Typespro 11:55
- 6 Configuring Argumentspro 07:52
- 7 Render Entry Footer Termspro 08:40
- 8 PHP 101: Concatenating Assignment Operatorpro 02:26
- 9 PHP 101: Building Stringspro 08:19
- 10 Test Entry Footer Termspro 03:03
- 11 Flush Rewrite Rulespro 03:56
- 12 Wrap it Uppro 03:10