Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

concatinating strings 2

Status
Not open for further replies.

TheConeHead

Programmer
Aug 14, 2002
2,106
US
is this valid in php:

$var = "This is ";
$var += "a sentence.";

thus:

$var would be: "This is a sentence.";

if not, what would the alternative be?

[conehead]
 
$var .= "a sentence.";

or

$var = "$var a sentence.";

or

$var = $var. "a sentence.";

Known is handfull, Unknown is worldfull
 
ah, didnt notice ur reply...

Known is handfull, Unknown is worldfull
 
No, it is not. The + is an arithmetic operator used to summing up variables. The concatenating operator in php is dot (.) so correct code would be:
Code:
$var  = "This is ";
$var .= "a sentence.";
Find out more about operators here (concatenating is under string operators):
 
I strongly recommend against that. PHP, even with the brackets, can be picky about associative array references inside quoted strings.

Anyway, except for strings containing escaped characters ("\n", "\t", etc) or SQL queries that use single quotes internally, I recomend using single-quotes around string literals. If you're outputting HTML, it can greatly improve readability to not have to escape all your internal doublequotes.


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
sleipnir214 said:
If you're outputting HTML, it can greatly improve readability to not have to escape all your internal doublequotes.
Albeit one can opt to use single quotes in HTML and double quotes in PHP and avoid escaping that way.
 
True. But most people use double quotes for HTML parameter values.

I've even heard of some interpretations of XHTML that require doublequotes around parameter values.


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top