...

View Full Version : what is $var = &$var



Arcticwarrio
07-10-2012, 04:27 PM
i cant remember where i got this from quite a while ago:



if ($_SERVER['REQUEST_METHOD'] == 'POST'){
$_METHOD = &$_POST;
}else{
$_METHOD = &$_GET;
}
db('open');
if (isset($_METHOD['save'])) {
//blah blah
}
}


and i adapted it to change the variable name for creating 2 tables from 1 query with


while ($HDRow = mysql_fetch_array($HDResults)) {

if ($HDRow['HDOPEN'] != 'Fixed'){$Output = &$Content;}else{$Output = &$Content2;} $Output .=' blah blah';
}
}

which sets the values of either var $Content or $Content2 depending on the db entry

however,

what is this method called?
i've googled all sorts and the closest thing i found was dynamic variables, but nothing with an & before the var name

any ideas?

Keleth
07-10-2012, 04:32 PM
It's assignment by reference, meaning rather then $_METHOD being its own variable, it now points to $_POST, so changes to $_METHOD change $_POST. I don't quite get the post for this particular use, as the idea behind it is you can alter the resulting variable and it affects the parent and the first example you showed doesn't make any use of that.

I've never seen it come in the second format though (to the right of the assignment operator), so I have no real idea what that's doing... in fact, it doesn't make any sense as far as I know, but maybe its a different use. In fact, I'm not sure how you can set something to the right of the assignment operator. In general, that second code segment doesn't make sense to me, so hopefully someone else can explain it better. Even the number of curly braces don't match up.

Fou-Lu
07-10-2012, 04:37 PM
Its called assignment by reference. Setting two variables to effectively point at the same memory space (which isn't actually true, but that's the effect we get):


$a = 'cat';
$b = 'mouse';
$c = &$a; // cat

$c = 'dog';
print $a; // dog


Simple process. Functions can also be written to both accept a reference and to return one. Accepting is easy, its exactly the same except you add a & to the variable in the function signature. Now any changes within the function reflect the passed argument as well.

Returning is more complicated.


function &myfunc()
{
static $i = 0;
return ++$i;
}

$a = &myfunc();
$b = &myfunc();
$c = &myfunc();

printf('a = %d, b = %d, c = %d', $a, $b, $c); // a = 3, b = 3, c = 3

Limited in it usefulness as it is, I have made use of return by reference in the past for shutdown results.

Arcticwarrio
07-10-2012, 04:40 PM
It's assignment by reference, meaning rather then $_METHOD being its own variable, it now points to $_POST, so changes to $_METHOD change $_POST. I don't quite get the post for this particular use, as the idea behind it is you can alter the resulting variable and it affects the parent and the first example you showed doesn't make any use of that... the second is harder to analyze due to its length.

i know what they do, and i wrote the 2nd snippet,

i was just nerding it wondering what it was called :) ie, assignment by reference

i have 2 tables on the page filled from a database,
the 2nd snippet puts the rows in the right table based on the db entry
$Output would contain '<tr><td>blah blah</td></tr>' etc


table 1:
<table>
echo $Content
</table>

table 2:
<table>
echo $Content2
</table>

Keleth
07-10-2012, 05:46 PM
Like I mentioned in my edit, the problem is I don't quite understand what the second code block would do, given the ampersand is to the right of the assignment operator... does that second code block work? Fou, do you have any idea what the ampersand to the right of the assignment does?

Arcticwarrio
07-10-2012, 05:57 PM
Like I mentioned in my edit, the problem is I don't quite understand what the second code block would do, given the ampersand is to the right of the assignment operator... does that second code block work? Fou, do you have any idea what the ampersand to the right of the assignment does?

yes its fine you already answered my question
Q) what is $var = &$var
A) assignment by reference

basically if $HDRow['HDOPEN'] = 'Fixed'
then $Output would be replaced by $Content2
else $Output would be replaced by $Content

it renames a variable for want of a better term

for your reference my code is here and works fine:


db('open');
$HDResults = query("SELECT * FROM `helpdesk`", __LINE__, __FILE__);
db('close');

$Content .='<table border="1" cellpadding="0" cellspacing="0">';
$Content .= '<caption> Current Problems </caption>';
$Content2 .='<table border="1" cellpadding="0" cellspacing="0">';
$Content2 .= '<caption> Previous Problems </caption>';
$Content1 .='<tr><th>';
if (check_access('16') == 1){$Content1 .='Hours';}
$Content1 .='</th>
<th>USER</th>
<th>STATUS</th>
<th>CATEGORY</th>
<th>PROBLEM</th>
<th>TICKET NO</th>
<th>ADMIN COMMENT</th>
<th>';
if (check_access('16') == 1){$Content1 .='ADMIN';}
$Content1 .='</th><th></th></tr>';
$Content .= $Content1;
$Content2 .= $Content1;
while ($HDRow = mysql_fetch_array($HDResults)) {

// HDID | HDSB | HDOPEN | HDCAT | HDPROB | HDTICKET | HDCOMMENT | HDADMIN
$phpdate = strtotime($HDRow['HDDATE']);
$age = strtotime("now") - $phpdate;
$age = round($age/(60*60),2);

$phpdate2 = strtotime($HDRow['HDFIXED']);
$age2 = $phpdate2 - $phpdate;
$age2 = round($age2/(60*60),2);

if ($HDRow['HDOPEN'] != 'Fixed'){$Output = &$Content;}else{$Output = &$Content2;} $Output .='
<form name="Form'.$HDRow['HDID'].'" action="" method="GET"><tr>
<td align="center">';
if (check_access('16') == 1){
if ($HDRow['HDFIXED'] == ''){
$Output .= $age;
}else{
$Output .= $age2.'<input type="hidden" name="HDFIXED" value="'.$phpdate2.'">';
}
}
$Output .='<input type="hidden" name="Update" value="'.$HDRow['HDID'].'"></td>
<td align="center" width="100">'.$HDRow['HDSB'].'</td>';
if (check_access('16') == 1){
switch ($HDRow['HDOPEN']){
case 'Not Fixed': {
$Output .='<td align="center" bgcolor="#FF0000" width="100">
<select name="HDOPEN" id="HDOPEN" style="border:none;background-color:#FF0000">
<option value="Not Fixed" selected="selected">Not Fixed</option>
<option value="In Progress">In Progress</option>
<option value="Fixed">Fixed</option>
</select>
</td>';break;}
case 'In Progress': {
$Output .='<td align="center" bgcolor="#FFCC66" width="100">
<select name="HDOPEN" id="HDOPEN" style="border:none;background-color:#FFCC66">
<option value="Not Fixed">Not Fixed</option>
<option value="In Progress" selected="selected">In Progress</option>
<option value="Fixed">Fixed</option>
</select></td>';break;}
case 'Fixed': {
$Output .='<td align="center" bgcolor="#66FF00" width="100">
<select name="HDOPEN" id="HDOPEN" style="border:none;background-color:#66FF00">
<option value="Not Fixed">Not Fixed</option>
<option value="In Progress">In Progress</option>
<option value="Fixed" selected="selected">Fixed</option>
</select></td>';break;}
default: {$Output .='<td align="center">See Graeme</td>';break;}
}
}else{
switch ($HDRow['HDOPEN']){
case 'Not Fixed': {$Output .='<td align="center" bgcolor="#FF0000" width="100">Not Fixed</td>';break;}
case 'In Progress': {$Output .='<td align="center" bgcolor="#FFCC66" width="100">In Progress</td>';break;}
case 'Fixed': {$Output .='<td align="center" bgcolor="#66FF00" width="100">Fixed</td>';break;}
default: {$Output .='<td align="center">See Graeme</td>';break;}
}
}

switch ($HDRow['HDCAT']){
case '0': {$Output .='<td align="center"></td>';break;}
case '1': {$Output .='<td align="center" bgcolor="#00FF00">Computer</td>';break;}
case '2': {$Output .='<td align="center" bgcolor="#00FFFF">Server</td>';break;}
case '3': {$Output .='<td align="center" bgcolor="#FFFF00">Emails</td>';break;}
case '4': {$Output .='<td align="center" bgcolor="#CCCCFF">Network</td>';break;}
case '5': {$Output .='<td align="center" bgcolor="#FFCCFF">Internet</td>';break;}
case '6': {$Output .='<td align="center" bgcolor="#FFCC33">Software</td>';break;}
case '7': {$Output .='<td align="center" bgcolor="#CCFF66">Printer</td>';break;}
case '8': {$Output .='<td align="center">Other</td>';break;}
default: {$Output .='<td align="center"></td>';break;}
}
$Output .='<td align="center" width="300">'.$HDRow['HDPROB'].'</td>
<td align="center">'.$HDRow['HDID'].'</td>
<td align="center" width="300">';
if (check_access('16') == 1){
if ($HDRow['HDOPEN'] != 'Fixed'){
$Output .='<input type="text" size="55" name="HDCOMMENT'.$HDRow['HDID'].'" value="'.$HDRow['HDCOMMENT'].'">';
}else{$Output .= '<input type="hidden" name="HDCOMMENT'.$HDRow['HDID'].'" value="'.$HDRow['HDCOMMENT'].'">'.$HDRow['HDCOMMENT'];}
}
else{$Output .= $HDRow['HDCOMMENT'];}

$Output .='</td>
<td align="center">';
if (check_access('16') == 1){$Output .=$HDRow['HDADMIN'].'</td><td><input class="Button" type="submit" name="Submit" value="Save">&nbsp;<input class="Button" type="submit" name="Delete" value="Delete"></form></td>';}
else{$Output .='</td><td></form></td>';}
$Output .='</tr>';
}

if (check_access('16') == 1){$Content .='</table><form action="" method="POST"><table><tr><td colspan="8">Add a Problem</td></tr>';}

else {$Content .='</table><form action="" method="POST"><table><tr><td colspan="6">If your problem isn\'t listed fill out the form below</td></tr>';}

$Content .='
<th></th>
<th>Category</th>
<th>Problem</th>
<th></th>
<tr><td align="center"></td><td align="center"><select name="HDCAT" id="HDCAT">
<option value="0" selected="selected">Select Category</option>
<option value="1">Your Computer</option>
<option value="2">Server</option>
<option value="3">Emails</option>
<option value="4">Network</option>
<option value="5">Internet</option>
<option value="6">Software</option>
<option value="7">Printer</option>
<option value="8">Other</option>
</select></td>
<td align="center"><textarea name="HDPROB">'.$_POST['HDPROB'].'</textarea></td>
<td><input class="Button" type="submit" name="Submit" value="Submit"></td>
</tr>

</table></form>&nbsp;<p>&nbsp;<p>&nbsp;';

$Content .= $Content2;
$Content .= '</table>';

include ("template.php");


i know its probably not the best way to write it but hey, its all i know :)

Fou-Lu
07-10-2012, 05:58 PM
Right of assignment is assignment by reference.
Placed on the left of the assignment operator it becomes shorthand notation for bitand calculations:


$a = 3;
$b &= 4;
print $b; // 0



BTW, you can always rewrite using temporary variables within a loop to not use references at all. If it works, I wouldn't change it since using references are perfectly fine to use, and PHP's copy on write context for its variables already provide an awesome optimization route for it. The only thing that you cannot (at least that I've found) simulate is a return by reference without actually using it; I can get away otherwise with assignments and standard returns, although given my OO nature now, I do a lot of by reference and by object pointer.

Keleth
07-10-2012, 06:40 PM
Wow, I am an idiot, heh... I answered the question only to ask it again because I didn't think though what I was asking or reading... how do I "unrep" myself? I deserve to lose a thanks for that one :p



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum