Mastering PHP: How many ways can you string a sentence together?
Tags: php, softwaredevelopment, programming, phpdevelopment
Published: 2019-03-15 12:51:22

---
title: Mastering PHP: How many ways can you string a sentence together?
published: true
tags: php,software-development,programming,php-development
canonical_url: 
---

![](https://cdn-images-1.medium.com/max/1024/0*90CHU_MnGSXClZKu.)<figcaption>“Close-up of lines of code on a computer screen” by <a href="https://unsplash.com/@ilyapavlov?utm_source=medium&amp;utm_medium=referral">Ilya Pavlov</a> on <a href="https://unsplash.com?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption>

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.

{% gist https://gist.github.com/peterfox/9b53b9145625e4e9e11933acd6d1ed70 %}

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.

{% gist https://gist.github.com/peterfox/f8d4dad94d6c29c1eb8776af3b4dec8f %}

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.

{% gist https://gist.github.com/peterfox/bed81388aec06fda55da2eb52d6b1886 %}

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.

{% gist https://gist.github.com/peterfox/9e5af3e9bc1053e814eb8abec51d24e0 %}

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.

{% gist https://gist.github.com/peterfox/79267d715731c016fe02b0868653bf60 %}

### 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](https://nikic.github.io/2012/01/09/Disproving-the-Single-Quotes-Performance-Myth.html) 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](https://www.peterfox.me) and feel free to follow me @[SlyFireFox](https://twitter.com/SlyFireFox) on twitter for more Laravel tips and tutorials.