Welcome to WebmasterWorld Guest from 54.167.102.69

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

How to add text inside variable?

     
11:41 am on Jan 13, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:Oct 25, 2006
posts: 994
votes: 0


I have variable and inside this variable I like to set variable.

How to do this?
$myvariable1 = "$myvariable2 [0],facebook marketing";

How to do validated code for this that string will be seen with $myvariable2 [0]
12:03 pm on Jan 13, 2011 (gmt 0)

Junior Member

10+ Year Member

joined:Mar 5, 2004
posts:198
votes: 0


PHP eval is the function you need: [php.net...]
12:19 pm on Jan 13, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:Feb 22, 2009
posts:1396
votes: 0


Hi all,

Unless I have misread the intention here, your nearly there with the example you have given:-

$myvariable1 = $myvariable2[0]."facebook marketing";

echo $myvariable1;

This would then print (for arguments sake) something like this:-

Contents of variable facebook marketing

if your joining variables together, you need to use the '.' full stop, this then concatenates the values together and stores them into the new name as you have assigned them to using the '=' assignment operator.

Cheers,
MRb
5:28 pm on Jan 14, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Nov 28, 2004
posts:7999
votes: 0


A second example, keeping in mind that scalar variables will interpolate with double quotes.

$fruits = Array (
'one' => 'cherries',
'two' => 'apples',
'three' => 'pears'
);

$animals = Array('lions','tigers','bears');

The following are equivalent, one may be more legible and easier to debug for you than the other depending on your style.

$output = "<p>Joebob walked his " . $animals[1] . " to the store to buy " . $fruits['one'] . ".</p>";

$myanimal = $animals[1];
$myfruit = $fruits['two'];

$output = "<p>Joebob walked his $myanimal to the store to buy $myfruit.</p>";