Unlock your potential with a Pro Membership
Here is where you propel your career forward. Come join us today. Learn more.
Specifying Which Argument in a Formatted String
Lab: PHP String Building and Processing Basics
Video Runtime: 04:18
What if you need to repeat one of the arguments within the formatted string? You could repeat the argument in the list again like this:
$message = sprintf( 'Welcome %s, your user id is %s. Hey, %s, ', $current_user['name']['first_name'], $user_id, $current_user['name']['first_name'] );
But then that’s a WET pattern, as you are repeating code.
Quick Tip: Don’t repeat yourself
Never repeat yourself. Don’t do it. If the code repeats, then refactor it.
Don’t repeat yourself.
Don’t repeat yourself.
Don’t repeat yourself.
Instead, PHP lets you specify which argument you want for each of the placeholders.
Resources
sprintf
– in Docxsprintf
– PHP Manualprintf
– PHP Manual- Type Casting
Your best friend is code, Tonya is making the introductions.
Episodes
Total Lab Runtime: 02:50:33
- 1 Lab Introductionfree 09:39
- 2 Embedding Variables in a Stringpro 15:16
- 3 Embedding Complex Variablespro 13:37
- 4 Concatenating Strings with a Dotpro 08:47
- 5 Concatenating and Assigning Shorthandpro 05:58
- 6 Formatting a String using Placeholderspro 15:16
- 7 Specifying Which Argument in a Formatted Stringpro 04:18
- 8 Has Substringpro 21:47
- 9 Replacing Substringspro 13:03
- 10 Get the String's Lengthpro 14:11
- 11 Character Set Encoding - It Matters!pro 11:42
- 12 Has Substring - for UTF-8pro 16:45
- 13 Replacing a UTF-8 Substringpro 05:56
- 14 Stripping out Characters or Entitiespro 10:51
- 15 Wrap it Upfree 03:27