Log in

View Full Version : Putting variables inside variables?



TheJoshMan
10-12-2008, 03:11 PM
Is this possible? I'm working with Wordpress for my site, and wanted to do some customization of my theme. The theme displays a tiny bit of info about the author of each post like the username they login with, how many posts they've written, and if they give a "bio" about themselves it includes that too...

What I want to do is to add a small bit of funny text below where it tells how many posts the user has made via PHP. I've found a script I'm using right now which I have gotten to work just fine. The problem is, some of the phrases I want to use would require to display the user's name (their login name), but I can't seem to figure out how to put that variable inside the array of things to say.

Here's the code, then below that I'll post the variation I tried to call the users' names.
The first code is what wordpress uses internally to call the post author's username/nickname
The second code is the random phrase script


<?php echo $curauth->nickname; ?>





<?php
$slyterm[] = '...We think';
$slyterm[] = 'Yes, we\'re counting.';
$slyterm[] = 'Support your <del>local</del> <strong><del>global</del></strong> <strong>GALACTIC</strong> writer people!';
$slyterm[] = 'We are assuming this one was written with "love" as well... (no guarantees)';
$slyterm[] = 'The buzz on the street is that this post was written with <em>love</em>...';

srand ((double) microtime() * 1000000);
$random_number = rand(0,count($slyterm)-1);
echo ($slyterm[$random_number]);
?>


Now here is what I added to try and call the user's nickname into the array...



$slyterm[] = 'We believe that $curauth->nickname only writes these to get the attention they were lacking as a child...';

Nile
10-12-2008, 10:03 PM
$slyterm[] = "We believe that $curauth->nickname only writes these to get the attention they were lacking as a child...";

Twey
10-12-2008, 10:29 PM
$slyterm[] = sprintf('We believe that %s only writes these to get the attention they were lacking as a child...', $curauth->nickname);

TheJoshMan
10-12-2008, 11:22 PM
$slyterm[] = "We believe that $curauth->nickname only writes these to get the attention they were lacking as a child...";


that outputs like this...



We believe that only writes these to get the attention they were lacking as a child...

TheJoshMan
10-12-2008, 11:23 PM
$slyterm[] = sprintf('We believe that %s only writes these to get the attention they were lacking as a child...', $curauth->nickname);


This one also outputs the same as above, it just completely removes that section that should display the post author's nickname.



We believe that only writes these to get the attention they were lacking as a child...

Twey
10-13-2008, 08:10 AM
That means that $curauth->nickname is empty.

Nile
10-13-2008, 11:46 AM
Run this code:


if(empty($curauth->nickname) || !isset($nickname)){
die('It is empty, or got unset.');
}
echo "It's fine, its not unset, or empty";

Tell us the output.

bluewalrus
10-13-2008, 01:22 PM
$slyterm[] = "We believe that" . $curauth->nickname . "only writes these to get the attention they were lacking as a child...";
echo $slyterm[];

jimbob79
10-14-2008, 01:12 AM
<?php echo $curauth->nickname; ?>





<?php
$slyterm[] = '...We think';
$slyterm[] = 'Yes, we\'re counting.';
$slyterm[] = 'Support your <del>local</del> <strong><del>global</del></strong> <strong>GALACTIC</strong> writer people!';
$slyterm[] = 'We are assuming this one was written with "love" as well... (no guarantees)';
$slyterm[] = 'The buzz on the street is that this post was written with <em>love</em>...';

srand ((double) microtime() * 1000000);
$random_number = rand(0,count($slyterm)-1);
echo ($slyterm[$random_number]);
?>


Now here is what I added to try and call the user's nickname into the array...



$slyterm[] = 'We believe that $curauth->nickname only writes these to get the attention they were lacking as a child...';


Can I have your permission to use this script? I might like to use it for random quotes or something.

TheJoshMan
10-14-2008, 01:15 AM
umm... sure you can use it.... If you can use it. LOL

It's not my script man, so go right ahead. I can't even remember where I found it. I didn't see any disclaimer that came with it, so I'm assuming it's free for distribution.

TheJoshMan
10-14-2008, 01:16 AM
@Twey:

Do you mean that the variable "$curauth" is empty?
Or does that mean that there is no "nickname" set?

Twey
10-14-2008, 05:58 AM
With PHP, it could be either.

TheJoshMan
10-15-2008, 12:42 PM
i got it figured out...

i'll post up the solution i found when i get home, and maybe someone can showme a better/easier way to accomplish it.