DEV Community

Peter Fox
Peter Fox

Posted on • Originally published at Medium on

Mastering PHP: How many ways can you string a sentence together?

“Close-up of lines of code on a computer screen” by Ilya Pavlov on Unsplash

This title probably seems a bit stupid, every PHP developer knows how to concatenate strings surely? It’s worth thinking about in more depth though, how many different ways can we actually do it? Is there a right way to do it? Is there a time when I’ve even god forbid been doing it in away that’s harder that it needs to be? Which way will be easier to read when I come back to it in a year?

Ok, so lets get into what different ways I could come up with.

Sprintf

So lets start with the one that’s common across a few languages (it is more often known as printf), namely C. The sprintf function is pretty nice but isn’t often used in PHP. basically you provide a template and then the variables to populate your template with.

When you look at the syntax of it it’s pretty similar to how you would put together an SQL statement with a lot of database libraries. Different placeholders are used depending on the data type, %s is for strings and %d is for integers but there are a number of others.

Dot Operator

The dot operator is obviously the most common way of forming a string, it’s simple but honestly it’s not always the most readable. You simply put two strings together and works with double quotes and single quotes.

Don’t forget you also have the .= operator as well which acts simply like += or -=to be able to concatenate to the end of a string.

Double Quoted Strings

A double quoted string is often a very misunderstood part of PHP. I’ve lost count of the amount of times I’ve seen double quotes when in reality single quotes would have been better instead. I think partly this is because developers often don’t realise there’s even a difference. Viewing the example below you can see why there is.

PHP’s string in double quotes actually let you inject variables into them to be able to produce a new string. This actually requires extra processing (nothing that will destroy your servers but if your entire application made use of double strings *you could start* to have a noticeable amount of time spent processing them). While double quotes are useful I would be careful, I often prefer using Sprintf myself because when you start using long variables inside the double quotes it can become fairly unmanageable to read.

Update: If you’re outraged about the double quotes and single quotes comparison feel free to jump to the end.

Heredocs/Nowdocs

Now, this is one I personally don’t use, pretty much ever. I honestly can’t think of any time I’ve found this useful. It’s fine and it has it’s place but I feel like there’s fewer and fewer places you’d use it these days. What is a heredoc? It’s basically a way of making a large multi-line string like in the example below.

I’ve seen others use it for making large blocks of HTML or SQL statements but honestly I’d rather stick to using templating libraries and sql query builders.

Implode

Ok, this is a bit of a cheat, but if Sprintf is here then why not implode? If you haven’t used it before, implode (and explode) are two functions you should be using a lot with your arrays. It simply takes a array and a string, combining all the values together with that string in-between the values. This being the case we can use a space string between our chosen words to produce a string just like the other examples.

Conclusions

Never forget there’s always more than one way to do things and doing it that one way every time is not the best way either. Lots of scenarios will have better reasons to use a particular mechanism than another, especially when it comes to readability. There’s nothing worse than seeing a long string of text in a piece of code that spans multiple lines while opening and closing the brackets frequently. I actually find myself rarely using the dot operator despite it being the main way of doing it in PHP.

Updated due to reader responses

I’ve had a fair amount of push back on this article. Some politely pointing out the facts while providing further readers and some simply being rude and overly aggressive, taking a PHP gate keeper stance. Double quotes vs. single quotes being the primary issue that most readers might have with this article. I just want to make it clear that I stated this could have an affect, this is not saying it will, as always there are circumstances that can and will change this, I have now gone back and put emphasis on this. Ultimately that’s not what this article is about, at no point do I try to force an opinion down anyones throat and I could care less about your own if you haven’t the ability to put it across without making it some form of personal attack. I’m going to keep writing anyways.

If you care to read more about the whole double quotes vs. single quotes argument here’s one of many articles available.

I’m Peter Fox, a software developer in the UK who works with Laravel among other things. If you want to know more about me you can at https://www.peterfox.me and feel free to follow me @SlyFireFox on twitter for more Laravel tips and tutorials.

Top comments (1)

Collapse
 
ddziaduch profile image
Damian Dziaduch

Hi Peter.

Thanks for this.

I mostly use HEREDOC for SQL or to inject language in the PHP (JavaScript is good example). The great thing about it is that when you use delimiter SQL in PhpStorm it will turn on SQL Color Schema and code completion. The same goes with JavaScript by JS delimiter 🙂