Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the 'period' character (.) mean if used in the middle of a php string?

Here is some example code:

$headers = 'From: [email protected]' . "\r\n" .
    'Reply-To: [email protected]' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

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

For example,

"blabla" . "blabla" . "blablalba";
like image 496
mhz Avatar asked May 24 '11 00:05

mhz


People also ask

What is the use of period character in PHP?

It's the concatenation operator, concatenating both strings together (making one string out of two separate strings).

What does dot equal mean in PHP?

the " . = " operator is a string operator, it first converts the values to strings; and since " . " means concatenate / append, the result is the string " 120 ".

Does PHP have a period?

In PHP, the period is the concatenation operator. Putting the periods in tells PHP to concatenate "mod/" to $modarrayout and then concatenate the resulting string to "/bar. php" . See page String Operators.

Is a period a character?

Alternatively referred to as a full stop or dot, a period ( . ) is a punctuation mark commonly found on the same US QWERTY keyboard key as the greater than ( > ).


1 Answers

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:

echo "This is the result of the function: " . myfunction();

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:

echo "The result is: " . $result;

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

echo "The result is: $result";

Another hard to catch mis-use is this:

echo "The results are: " . $myarray['myvalue'] . " and " . $class->property;

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

echo "The results are: {$myarray['myvalue']} and {$class->property}";

About the example cited:

$headers = 'From: [email protected]' . "\r\n" .
    'Reply-To: [email protected]' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

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 "\r\n" 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.

like image 100
2 revs Avatar answered Sep 19 '22 15:09

2 revs