View Full Version : Basic string question

07-22-2008, 02:40 AM
Hey all i am new at PHP so be nice :thumbsup:

How can i do the following code in php?

dim strName
dim strTheName

strTheName = "BOB"

strName = "Welcome " & strTheName & " to your site!"
response.write strName

I've tried this:

$user_id_name = "BOB";

$html = 'Welcome ' & $user_id_name & ' to your site!"';
echo $html;

But that doesnt seem to display anything for the $user_id_name. just the Welcome and the to your site!.

Any help would be great! :)

07-22-2008, 02:59 AM
Almost, it should be

$user_id_name = "BOB";

$html = 'Welcome ' . $user_id_name . ' to your site!';
echo $html;

A dot ( . ) is the php equivalent of the asp ampersand ( & ) in this situation.

07-22-2008, 03:06 AM
Yep that did it binaryWeapon.

Thanks for the knowledge! :thumbsup:


07-22-2008, 01:44 PM
While the . is the string join function, a better way to accomplish this example would be:

$user_id_name = 'BOB';

echo 'Welcome ', $user_id_name, ' to your site!"';

Unless you really need the string stored as a variable, using echo with commas to separate the parts is faster and better. It will work with the string . to join the parts, but isn't as fast.

Also, the declaration of $user_id_name should be in single quotes, not double as you don't need to have PHP parse any variables inside. It will be faster as well.

Just a few tips to help you out.