Forum Moderators: coopster

Message Too Old, No Replies

print() and echo() question

         

jeremymgp

9:11 pm on Sep 26, 2004 (gmt 0)

10+ Year Member



Hi,

Here's my code:

<?php
$url1="http://www.****1.com/";
$url2="http://www.****2.com/";
$url3="http://www.****3.com/";
$charset="0321";
$random=$charset[rand(0,4)+1];
$urlname="url".$random;
print $url;
?>

The trouble is when I print $url, it actually prints the variable name "url2" or whatever. I want it to print the variable's value, so instead of printing "url2" it would print $url2's value - [****2.com...]

How can I do this? Kind of urgent, so thanks loads for your help,

Cheers,

Jeremy

[edited by: ergophobe at 10:33 pm (utc) on Sep. 26, 2004]
[edit reason] delinked url [/edit]

toltec75

9:17 pm on Sep 26, 2004 (gmt 0)

10+ Year Member



echo "$url1";

print "$url1";

mincklerstraat

8:38 am on Sep 27, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



echo $url1; should do it too.

jeremymgp

12:14 pm on Sep 27, 2004 (gmt 0)

10+ Year Member



Hi,

Thanks loads for the answers and mod-edit, I tried that solution again but it doesn't work.

Here's my updated code:
<?php
$url1="http://www.****1.com/";
$url2="http://www.****2.com/";
$url3="http://www.****3.com/";
$charset="0321";
$random=$charset[rand(0,4)+1];
$urlname="url".$random;
print "$urlname";
?>

I want to generate a random url from a list of 3, so I create the $urlname string which is made of "url" + a random number giving me "url2", "url3" or whatever. However, I don't want this to be a string, I need it to be a variable's name. So when I print "url2", it doesn't say "url2" but prints the VALUE of the url2 variable, which would be "http://www.****2.com". Even with the quotation marks it's still just showing the text string "url2"

This seems to be a newbie question, all I want to do is print variable values rather than variable names. Thanks again for your help,

Jeremy

Iguana

12:25 pm on Sep 27, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Have you tried
print $$urlname;

Bonusbana

12:26 pm on Sep 27, 2004 (gmt 0)

10+ Year Member



Maybe this would help:

$urls = array("http://www.****1.com/","http://www.****2.com/","http://www.****3.com/");
$urlname = $urls[rand(0,count($urls))];
echo $urlname;

jeremymgp

12:34 pm on Sep 27, 2004 (gmt 0)

10+ Year Member



Thanks Iguana and Bonusbana,

The double $$ worked a treat, now the value is showing. Also array solutions are generally much neater so I'll use them instead. My first PHP script finally works!

Thx guys,

Jeremy