Solved

Basic PHP question the use of $$

Posted on 2008-10-17
4
261 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
This article discusses four methods for overlaying images in a container on a web page
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

757 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

20 Experts available now in Live!

Get 1:1 Help Now