How to combine string values in php?

There are two string operators. The first is the concatenation operator ['.'], which returns the concatenation of its right and left arguments. The second is the concatenating assignment operator ['.='], which appends the argument on the right side to the argument on the left side. Please read Assignment Operators for more information.

K.Alex

9 years ago

As for me, curly braces serve good substitution for concatenation, and they are quicker to type and code looks cleaner. Remember to use double quotes [" "] as their content is parced by php, because in single quotes [' '] you'll get litaral name of variable provided:

anders dot benke at telia dot com

18 years ago

A word of caution - the dot operator has the same precedence as + and -, which can yield unexpected results.

Example:

The above will print out "3" instead of "Result: 6", since first the string "Result3" is created and this is then added to 3 yielding 3, non-empty non-numeric strings being converted to 0.

To print "Result: 6", use parantheses to alter precedence:

Stephen Clay

16 years ago


Use double quotes to concat more than two strings instead of multiple '.' operators.  PHP is forced to re-concatenate with every '.' operator.

hexidecimalgadget at hotmail dot com

13 years ago

If you attempt to add numbers with a concatenation operator, your result will be the result of those numbers as strings.

mariusads::at::helpedia.com

14 years ago

Be careful so that you don't type "." instead of ";" at the end of a line.

It took me more than 30 minutes to debug a long script because of something like this:

The output is "axbc", because of the dot on the first line.

biziclop

12 days ago

Some bitwise operators [the and, or, xor and not operators: & | ^ ~ ] also work with strings too since PHP4, so you don't have to loop through strings and do chr[ord[$s[i]]] like things.

See the documentation of the bitwise operators: //www.php.net/operators.bitwise



Live demo: //3v4l.org/MnFeb

What is the result of combining string with another data type in PHP?

A string and an integer value are added, and the result is an integer value. Concatenation operator ['.

How do I concatenate strings and variables in laravel?

how can i do that concatenation? – Saja. May 16, 2017 at 8:35..
Use + for variables and dot for string. – Sapnesh Naik. May 16, 2017 at 8:36..
You could change input['select_teacherActivity".$week_no.".$activity']; to input["select_teacherActivity$week_no$activity"]; [in double quotes] – Antonios Tsimourtos..

How do I concatenate a PHP variable in SQL query?

Applying Concatenation Functions for MySQL Queries.
Insert Query: INSERT INTO [field1] VALUES [CONCAT[string1,string2...]].
Update Query: UPDATE SET field1=CONCAT[string1,string2...].
Select Query: SELECT CONCAT[string1,string2...] FROM .

What is the PHP concatenation operator?

1. Concatenation Operator ["."]: In PHP, this operator is used to combine the two string values and returns it as a new string.

Chủ Đề