Solved

Basic PHP question the use of $$

Posted on 2008-10-17
4
270 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

WordPress Tutorial 4: Recommended Plugins

Now that you have WordPress installed, understand the interface, and know how to install new parts, let’s take a look at our recommended plugins.

Question has a verified solution.

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

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 …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

617 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