Solved

Basic PHP question the use of $$

Posted on 2008-10-17
4
262 Views
Last Modified: 2013-12-13
This is a basic PHP question.I am Java/C++ guy trying to understand a third party code and I am stuck at how things work in PHP

 if($person == 'male' ){
            $user = $$person;
            $name = '<b>'.ucwords($user->username).";
            return $name;
           
         
        }
I am wondering how $person which has a string can contain in  if($person == 'male' )
the comeplete user object in $$person.

Is it dereferencing (cant be) does $$ have a special meaning ..any example or online tutorial will be of a great help.

Thanks
   
0
Comment
Question by:micro_learner
  • 2
4 Comments
 
LVL 7

Accepted Solution

by:
mltsy earned 250 total points
ID: 22747044
This example doesn't make a lot of sense to me, but this is how $$ works:
http://us3.php.net/manual/en/language.variables.variable.php

Basically it first replaces $person with 'male' and then evaluates what remains: $male
0
 
LVL 18

Assisted Solution

by:Hube02
Hube02 earned 250 total points
ID: 22747237
As mltsy says, the example you give makes very little sense at all.

in this case you could simplify the statement in several ways.

first:

if($person == 'male' ){
  $user = $male;
  $name = '<b>'.ucwords($user->username).";
  return $name;
}

then:

if($person == 'male' ){
  $name = '<b>'.ucwords($male->username).";
  return $name;
}

then:

if($person == 'male' ){
  return = '<b>'.ucwords($male->username).";
}

.... there seems to be no reason to use the $$ in this case other than to make the code obtuse and harder to understand
0
 
LVL 27

Expert Comment

by:ddrudik
ID: 22747289
Since at soon as indexes are involved in the varname the concern is for ambiguity unless you use { }, possibly using { } is a good habit since code within { } is evaluated first.  Also I removed ." from the example since the double-quote seemed to be unmatched (maybe the example was trunacted before posting the question).
 if($person == 'male' ){

            $user = ${$person};

            $name = '<b>'.ucwords($user->username);

            return $name;

        }
 

or:
 

 if($person == 'male' ){

            return '<b>'.ucwords(${$person}->username);

        }

Open in new window

0
 
LVL 18

Expert Comment

by:Hube02
ID: 22748041
$$ is usually used when you will not know the exact value of something, which is why its use in the example you posted does not make sense.

switch ($person) {
  case 'male':
  case 'female':
    return '<b>'.ucwords(${$person}->username);
    break;
  default:
    // person has no gender
    // call routine to get the user's gender
}

in the above situation, while we know that $person can be either 'male' or 'female' when we use it in the return we don't know which one we will be returning. In this case it makes sense to use the $$ construct.

And ddrudik is correct, you should always use the curly brackets to make the code clearer. using ${$person} serves two purposes. First, it is more clear what we are doing, that is, using the value of a variable as a variable name. Second, we know by looking at it that this is not a typo and that the person that did the coding actually meant to use $$person and not $person.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now