Skip to content
Advertisement

What does the ‘period’ character (.) mean if used in the middle of a PHP string?

Here is some example code:

JavaScript

What does the period character do in the middle of each piece of the string?

For example,

JavaScript

Advertisement

Answer

This operator is used to combine strings.

EDIT

Well, to be more specific if a value is not a string, it has to be converted to one. See Converting to a string for a bit more detail.

Unfortunately it’s sometimes mis-used to the point that things become harder to read. Here are okay uses:

JavaScript

Here we’re combining the output of a function. This is okay because we don’t have a way to do this using the standard inline string syntax. A few ways to improperly use this:

JavaScript

Here, you have a variable called $result which we can inline in the string instead:

JavaScript

Another hard to catch mis-use is this:

JavaScript

This is a bit tricky if you don’t know about the {} escape sequence for inlining variables:

JavaScript

About the example cited:

JavaScript

This is a bit tricker, because if we don’t use the concatenation operator, we might send out a newline by accident, so this forces lines to end in “rn” instead. I would consider this a more unusual case due to restrictions of email headers.

Remember, these concatenation operators break out of the string, making things a bit harder to read, so only use them when necessary.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement