An experienced Senior Front End Web Developer

Include variables in a PHP string

This is just a quick tip, something quite basic but hopefully useful to you.

Did you know you can easily include variable values in a PHP echo string?

It works as long as you’re using double quotes to surround the string. So, you don’t have to do this:

You can use:

Contrast:

Oops! This one’s giving us an error. Why? The string is being terminated too early with the apostrophe on “he’s”. So we’d have to escape it:

But this isn’t what we want, so we’re back to the beginning with concatenating (joining together) the string with the variable.

So, Option 2 is really our best bet.

Note this even works with more complex variable access such as the following:

Or:

Find out more about the PHP string data type.

Comment on this:

*