Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Basic PHP question the use of $$

Posted on 2008-10-17
4
Medium Priority
?
271 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 1000 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 1000 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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 …
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…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

688 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