Monday, December 4, 2006

Concatenate Strings

when you need printing more than one strings where there may be variables or other things between them, it would be annoying to write a new statement to echo all the parts. php has a nice way of doing this for you, string concatenating
let me show you an example:

echo "you have " . $msg_num . "messages!" ;

the . operator is the string concatenation operator and is very useful for situations like this. ( i left spaces after and before '.' operators to make them more visible. this is not needed in fact. )
you could also write the statement above like that without string concatenating :

echo "you have $msg_num messages!" ;

this will give an output like:

you have 3 messages!

depending on the variable $msg_num .
be careful that you can do this only inside double quotes. if you use a statement like that:

echo 'you have $msg_num messages! ' ;

you 'll get html output exactly as in the quotes:

you have $msg_num messages!

there should be a value instead of $msg_num here so we should use double quotes for this statement. single quotes give output without changing.