DEV Community

Joe 🏴󠁧󠁢󠁷󠁬󠁳󠁿
Joe 🏴󠁧󠁢󠁷󠁬󠁳󠁿

Posted on

Write good PHP - string management

Working with strings is common in any language, but it's very easy to make it harder than it needs to be!

In PHP, it's very easy to work with strings, but it can be easier.

Let's dissect the below example of a common, but poor usage of strings in PHP:

$firstname = 'John';
$lastname = 'Doe';
$name = 'The user ' . $firstname . ' ' . $lastname . ' joined in ' . date('d/m/y') . '. Their birthday is ' . 32 - date('Y');
Enter fullscreen mode Exit fullscreen mode

This will output exactly what you expect, but wooow, that's ugly, isn't it? Inline calculations, multiple sets of concatenation, and it's longer than it needs to be.

Let's see a refactor and explain it:

$first_name = 'John';
$last_name = 'Doe';
$full_name = "{$first_name} {$last_name}";
$join_date = date('d/m/y'); 
$birthday = 32 - date('Y'); 
$name = "The user {$full_name} joined in {$join_date}. Their birthday is {$birthday}";
Enter fullscreen mode Exit fullscreen mode

We see more code, but it is much more readable, right?

What have we done here?

  • We've broken the dynamic values down to assigned variables outside of the string.
  • We've used interpolation to avoid concatenation. Interpolation means we can avoid the dot concat syntax, and awkward strings and spaces - making the string much much easier to manage.
  • Reduced the length of the string as a result of interpolation

Thanks to the above points, our refactor is much easier to read, and a lot easier to understand and edit.

We have introduced more code, but generally, readability should come before length. For beginners, it can be easy to fall into the trap of wanting to write really complicated code, in as few lines as possible - while cool, and very fun, it can easily get out of hand!

This has been a simple example of string management in PHP, I hope you enjoyed!

In the next part of this series, I'll cover writing logical structures with the happy path.

Top comments (4)

Collapse
 
kralik12 profile image
Michal Král

Have you considered talking about (s)printf functions? They are IMHO also much more readable than concatation.

Collapse
 
joemoses33 profile image
Joe 🏴󠁧󠁢󠁷󠁬󠁳󠁿

I've not! That might make for a good post, printX vs echo :)

Collapse
 
deta19 profile image
mihai

i accept your oppionion partialy. i thinks its better to use concatenation because it will make your strings morereadble. I know you prefer the double quotes, but if concatenation doesn;t effect the execution time, i prefer concateations because, in my opinion is easier to read, the variables pop up and i think its easier to debug because you 're sure the error comes from the variable and not a execution problem on the string part, those double quotes. Also junior devs will realise faster that you concatenate variable values to the string.
those are my 2cents

Collapse
 
joemoses33 profile image
Joe 🏴󠁧󠁢󠁷󠁬󠁳󠁿

I get your points!

I'd imagine the execution difference is absolutely trivial!

Concat is 8-11ms, whereas interpolation is 7-10. Definitely not a deciding factor, imo!

I think it comes down to personal preference! Though, for Jr Devs, I think we should always encourage them to learn and use (objectively and subjectively) clearer syntax.