Adding an underscore to the end of a variable does not work

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
flycast
Forum Commoner
Posts: 37
Joined: Wed Jun 01, 2005 7:33 pm

Adding an underscore to the end of a variable does not work

Post by flycast »

Code:

Code: Select all

$destUser = "theUser";
$destDBUser = "theDBUser";
echo "$destUser_$destDBUser";
Outputs:
theDBUser
not
theUser_theDBUser
Adding the underscore after the variable inside quotes supresses the variable in some way. Why?
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Adding an underscore to the end of a variable does not w

Post by Weirdan »

because underscore is a valid character in a variable name and it's interpreted as $destUser_'s value followed by $destDBUser's value.

use curly braces, like this:

Code: Select all

echo "{$destUser}_{$destDBUser}"
Post Reply