Solved

Basic PHP question the use of $$

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Suggested Solutions

Title # Comments Views Activity
Google Maps API and PHP 25 49
comma true 6 37
Add email address from a web page to outlook 2010 contacts ? 8 40
Custom Wordpress Loop 22 38
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
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 count occurrences of each item in an array.

809 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