When to use a variable variable in PHP?

The place for general PHP questions and hints for PHP on IBM i

When to use a variable variable in PHP?

Postby indr9864 on Wed May 31, 2017 9:03 am

I've been developing in PHP for a while now, and I still have not had a task where I've had to use variable variables. Can anyone give me examples where using them is a good idea ? Or were they included in the language for no reason ?
indr9864
 
Posts: 1
Joined: Wed May 31, 2017 9:01 am

Re: When to use a variable variable in PHP?

Postby anab6077 on Mon Jun 26, 2017 9:10 am

I also have the same problem. Looking help from someone.
anab6077
 
Posts: 1
Joined: Mon Jun 26, 2017 9:07 am

Re: When to use a variable variable in PHP?

Postby mani3164 on Thu Jun 29, 2017 10:50 am

Variable use Doller sign
mani3164
 
Posts: 1
Joined: Thu Jun 29, 2017 10:23 am

Re: When to use a variable variable in PHP?

Postby frie3144 on Sat Jul 08, 2017 6:44 am

Friendship day is celebrated on the first Sunday of August in every year. This day is also special for your friendship if you have the best friend or if you have any loyal, faithful friend.
frie3144
 
Posts: 2
Joined: Sat Jul 08, 2017 6:41 am

Re: When to use a variable variable in PHP?

Postby frie3144 on Sat Jul 08, 2017 7:01 am

Friendship day is celebrated on the first Sunday of August in every year. This day is also special for your friendship if you have the best friend or if you have any loyal, faithful friend.
frie3144
 
Posts: 2
Joined: Sat Jul 08, 2017 6:41 am

Re: When to use a variable variable in PHP?

Postby alek76 on Tue Aug 29, 2017 10:24 pm

I use those very rarely. Usually when you have some sort of condition:

Code: Select all
if ($a){
$varname = 'fullname';
} else {
$varname = 'name';
}
echo $$varname;


This is the same as

Code: Select all
if ($a){
echo $fullname;
} else {
echo $name;
}


And although this is a simple case in which there is no need to use the variable variable - you may get more complex cases where that is the only way to achieve the desired result.
alek76
 
Posts: 5
Joined: Tue Aug 29, 2017 9:57 pm


Return to PHP Questions / Hints

Who is online

Users browsing this forum: Google [Bot] and 2 guests

cron