Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    721
    Thanks
    20
    Thanked 84 Times in 84 Posts

    what is $var = &$var

    i cant remember where i got this from quite a while ago:

    PHP Code:
    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

    PHP Code:
    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?

  • #2
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,535
    Thanks
    45
    Thanked 259 Times in 256 Posts
    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.
    Last edited by Keleth; 07-10-2012 at 03:35 PM.

  • Users who have thanked Keleth for this post:

    Arcticwarrio (07-10-2012)

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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):
    PHP Code:
    $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.
    PHP Code:
    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.

  • Users who have thanked Fou-Lu for this post:

    Arcticwarrio (07-10-2012)

  • #4
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    721
    Thanks
    20
    Thanked 84 Times in 84 Posts
    Quote Originally Posted by Keleth View Post
    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

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

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

  • #5
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,535
    Thanks
    45
    Thanked 259 Times in 256 Posts
    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?

  • #6
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    721
    Thanks
    20
    Thanked 84 Times in 84 Posts
    Quote Originally Posted by Keleth View Post
    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:

    PHP Code:
    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

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Right of assignment is assignment by reference.
    Placed on the left of the assignment operator it becomes shorthand notation for bitand calculations:
    PHP Code:
    $a 3;
    $b &= 4;
    print 
    $b// 0 
    Edit:
    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.
    Last edited by Fou-Lu; 07-10-2012 at 05:01 PM.

  • #8
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,535
    Thanks
    45
    Thanked 259 Times in 256 Posts
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •