Odd Variable Question

ChrispyChrispy Registered User regular
edited June 2008 in Help / Advice Forum
So, I'm going to get right to the point of the question, then explain it the best I can.

Is there a way in PHP, not that that matters really, to compare two variable NAMES and not the actual values of the variables?


Let me set this up, please excuse the crude-ness of the code:

// These are times for a work schedule. Requested means that that time cannot be changed, I.E. cannot switch with a fellow employee. Where Normal can be changed, I.E. can switch with an employee.
[PHP]$RequestedMorning = "9 - 5";
$NormalMorning = "9 - 5";[/PHP]

// This array holds the schedule for a single person of the course, in this case, 4 days.
[PHP]$Schedule = array ($RequestedMorning,$RequestedMorning,$NormalMorning,$RequestedMorning);[/PHP]

// I now want to print this persons schedule out and have the Requested stuff in red and the normal stuff in black.
[PHP]for($counter = 0 ; $counter < count($Schedule) ; $counter++)
{ if($Schedule[$counter] == $RequestedMorning)
{ <font color="red"> echo $Schedule[$counter];
}
else
{ echo $Schedule[$counter];
}
}[/PHP]

The problem though is that $RequestedMorning and $NormalMorning have the same value and so the if part will always be true and always print in red. I want to check to see if the variable in the array is the same variable, not the value in the array is the same value. Can anyone help?

Thanks in advance

~ Make life how you want it ~
Chrispy on

Posts

  • PopsPops __BANNED USERS regular
    edited June 2008
    Compare two variable names?

    In most languages I've seen variable names don't really exist at runtime.

    Pops on
  • SeñorAmorSeñorAmor !!! Registered User regular
    edited June 2008
    You could put the variables in single quotes and compare the strings.


    Unless I'm not understanding what you're talking about.

    SeñorAmor on
  • ChrispyChrispy Registered User regular
    edited June 2008
    I see what you're saying SeñorAmor, but in my case, I have a ton of different schedule times and I think doing it your way makes me need to double the amount of variables I have. Not that that is a problem, I just don't want to use so many.

    Chrispy on
    ~ Make life how you want it ~
  • TheCapnTheCapn Registered User regular
    edited June 2008
    You could do it if you could compare memory addresses, but I don't know how to do that in PHP.

    I.E., in Java,
    String one = "hi";
    String two = "hi";
    System.out.print(one == two);
    

    would give you "false".

    TheCapn on
    Tacopants - a tasty comfortable combination.
  • DrFrylockDrFrylock Registered User regular
    edited June 2008
    I think I understand what you're asking for, and what you're asking for won't work. Basically, you want to see if $Schedule[$counter] came from a $RequestedMorning or a $NormalMorning variable somewhere earlier in the code. However, $Schedule[$counter], $RequestedMorning, and $MaybeSomeOtherVariables are all just aliases for a piece of data.

    If you want to keep some additional metadata around, such as whether a particular schedule entry was a regular or a normal entry, then you need to keep that in a separate variable. You can keep the two things in a structure together.

    Note that in many languages (Java, Python, other dynamic languages) there are reflection capabilities that can give you the ability to suss out variable names in certain contexts, but this is not a place where you would use reflection.

    DrFrylock on
  • Premier kakosPremier kakos Registered User, ClubPA regular
    edited June 2008
    I believe this is what you want...
    $refRequestedMorning = "RequestedMorning";
    $refNormalMorning = "NormalMorning";
    
    $$refRequestedMorning = "9 - 5";
    $$refNormalMorning = "9 - 5";
    
    $Schedule = array ($refRequestedMorning,$refRequestedMorning,$refNormalMorning,$refRequestedMorning);
    
    for($counter = 0 ; $counter < count($Schedule) ; $counter++)
    {
            if($Schedule[$counter] == $refRequestedMorning)
            {
                    echo "<font color=\"red\">", $$Schedule[$counter], "</font>\n";
            }       
            else
            {
                    echo $$Schedule[$counter], "\n"; 
            }
    }
    

    You can store variable names in a variable and then "dereference" them with the $$myVariableName syntax. So, the key here is to store the ref in your array and then you can just do a string compare of the variable names and then deref them when you print the value you wanted.

    Premier kakos on
  • DrFrylockDrFrylock Registered User regular
    edited June 2008
    Stuff

    Now I'm not an expert in PHP, but in PHP-land is this a common design pattern to solve this particular problem? Because it seems completely bizarre.

    DrFrylock on
  • Premier kakosPremier kakos Registered User, ClubPA regular
    edited June 2008
    DrFrylock wrote: »
    Stuff

    Now I'm not an expert in PHP, but in PHP-land is this a common design pattern to solve this particular problem? Because it seems completely bizarre.

    They're properly called "variable variables" (Stupid name, I know. They're soft references, PHP morons!), but they do see quite a bit of use. They're pretty handy to have around when you need to solve certain problems.

    Premier kakos on
  • ChrispyChrispy Registered User regular
    edited June 2008
    Premier kakos: I'll have to try that out. Thanks all for you help.

    Chrispy on
    ~ Make life how you want it ~
Sign In or Register to comment.