PDA

View Full Version : Simple problem... Seperating PHP variables in strings



sirbrent
04-07-2008, 12:22 AM
Hi,
I have a simple script that spits out an html table,
i am having some problem with how to insert the variable correctly in this situation (i am new to php)

so, there is a variable set by a included script
it sets this:

$userdir = "www.example.com/user/";

the script thats generating the table has this line


tableoutput .= "<div class=\"hcbfooter\" style=\"width: 150px; margin-top: 0px; margin-left: auto; margin-right: auto; margin-bottom: 5px; text-align: center; background-image: url($userdirpic2008.jpg); height: 200px;\"></div>";

but that doesnt work, it sees the variable as $userdirpic2008,
i cant figure out how to seperate the variable from the rest of the string, without making it stop working.

i have tried some stuff

i thought i has seen this somewhere, but it didnt work:

$userdir.pic2008
and i cant seperate it with a "/" because there is already one trailing the variable's value (which i cant change because other stuff relies on it):

i am sure there is a simple answer, i just dont know it... and google failed me this time :-(

Thanks for your help.

linFox
04-07-2008, 03:14 AM
To join strings and variables, you do use the period as the connection, however you just need to make sure the strings are seperated.
Eg. $string = "string".$var."string";
So, use this line:

$tableoutput .= "<div class=\"hcbfooter\" style=\"width: 150px; margin-top: 0px; margin-left: auto; margin-right: auto; margin-bottom: 5px; text-align: center; background-image: url(".$userdir."pic2008.jpg); height: 200px;\"></div>";

I concatenate all of my strings and variables like this in all of my code (rather than simply using them directly in the string), to avoid this sort of issue ever occurring.

dob99
04-18-2008, 06:20 PM
Or use "...url({$userdir}pic2008.jpg);..." if you don't like concatenation.

pghcollectibles
04-19-2008, 11:51 AM
can you use the bracket instead of double quotes and period in every instance of concatenation? in this link: http://en.wikipedia.org/wiki/Concatenation?
in the part of interpolation, it also shows this:

my $stringVar;
$stringVar = "World";
print "Hello $stringVar";

can you insert the string literal into the other variable in a case like this then also?

tssource
04-19-2008, 02:13 PM
Yes, you can...The problem you had though when you inserted the variable into your string without ending the string before concatenating the variable was that, when you put $userdirpic2008.jpg, PHP was trying to find the variable "$userdirpic2008" Which, in this case, there is no such variable. Instead, I would value $userdir as "www.example.com/user" (without the slash) This way, you can use url($userdir/pic2008.jpg), and this would work fine.