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
Posts
In most languages I've seen variable names don't really exist at runtime.
Unless I'm not understanding what you're talking about.
I.E., in Java,
would give you "false".
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.
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.
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.