Php insert variable into string

I know this is an old question, but I think someone has to mention all pros & cons:

Better Syntax: That's personal preference.

Performance: No difference. As many mentioned, double-quote might be faster if using unrealistically many variables.

Better Usage: Single quote [mostly]. As @Khez said, with single quote you can concatenate anything, even function calls and variable modification, like so: echo 'hi ' . trim[$name] . [$i + 1];. The only thing double-quote can do that single-quote cannot do is usage of \n, \r, \t and alike.

Readability: No difference [may personal preference apply].

Writability/Re-Writability/Debugging: In 1-line statements there is no difference, but when dealing with multiple lines, it's easier to comment/uncomment lines while debugging or writing. For example:

$q = 'SELECT ' .
     't1.col1 ' .
     ',t2.col2 ' .
   //',t3.col3 ' .
     'FROM tbl1 AS t1 ' .
     'LEFT JOIN tbl2 AS t2 ON t2.col2 = t1.col1 ' .
   //'LEFT JOIN tbl3 AS t3 ON t3.col3 = t2.col2 ' .
     'WHERE t1.col1 = ' . $x . ' ' .
     '  AND t2.col2 = ' . $y . ' ' .
   //'  AND t3.col3 = ' . $z . ' ' .
     'ORDER BY t1.col1 ASC ' .
     'LIMIT 10';

Less Escaping: Single-quote. For single quote you need to escape 2 characters only [' and \]. For double quote you need to escape 2 characters [", \] and 3 more if required [$, { and }].

Less Changes: Single quote. For example if you have the following code:

echo 'Number ' . $i . '!';

And you need to increment 1 to $i, so it becomes likes:

echo 'Number ' . [$i + 1] . '!';

But for double quote, you will need to change this:

echo "Number $i!";

to this:

echo "Number " . [$i + 1] . "!";

Conclusion: Use what you prefer.

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

20 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

How do you pass a variable inside a string in PHP?

In PHP, variables can be parsed within strings specified with double quotes [ " ]. This means that within the string, the computer will replace an occurence of a variable with that variable's value.

How do you call a variable in PHP?

PHP Variables.
A variable starts with the $ sign, followed by the name of the variable..
A variable name must start with a letter or the underscore character..
A variable name cannot start with a number..
A variable name can only contain alpha-numeric characters and underscores [A-z, 0-9, and _ ].

How do I echo text and variable in PHP?

The echo[] function outputs one or more strings. Note: The echo[] function is not actually a function, so you are not required to use parentheses with it. However, if you want to pass more than one parameter to echo[], using parentheses will generate a parse error.

What are string variables in PHP?

String is one of the data types supported by PHP. The string variables can contain alphanumeric characters. Strings are created when; You declare variable and assign string characters to it. You can directly use PHP Strings with echo statement.

Chủ Đề