Jump to content

Is this legal?


danp_canucks

Recommended Posts

instantiate variable by reference: ie

 

 



<?



function test(& $var1, & $var2){

print gettype($var1);  //outputs NULL
        print gettype($var1);  //outputs NULL

$var1="VAR 1";
$var2="VAR 2";

} 

print $var1."   ".$var2;

//OUTPUT: Notice: Undefined variable: var1 in C:\Inetpub\wwwroot\work\test.php on line 12

//        Notice: Undefined variable: var2 in C:\Inetpub\wwwroot\work\test.php on line 12

test($var1, $var2);
print $var1."   ".$var2;

//OUTPUT: VAR 1   VAR 2

 

 

This was tested on two different platforms and seems to be stable.  Thus it leads me to think it's valid to do this.  It seems that PHP supports a  built-in pointer to NULL which seems to make sense.  I would like for someone to confirm or refute this statement. 

 

 

 

 

Link to comment
Share on other sites

When you reference an uninitialized variable, php initializes it for you, with a value of NULL. If you remove the first 'print', you'll find that it doesn't even print any notices. If you try to pass an uninitialized variable by reference, it is assumed to be desired behaviour.

 

Some internal functions (such as preg_match_all) use this same feature.

 

<?php
// The \\2 is an example of backreferencing. This tells pcre that
// it must match the second set of parentheses in the regular expression
// itself, which would be the ([\w]+) in this case. The extra backslash is
// required because the string is in double quotes.
$html = "<b>bold text</b><a href=howdy.html>click me</a>";

preg_match_all("/(<([\w]+)[^>]*>)(.*)(<\/\\2>)/", $html, $matches, PREG_SET_ORDER);

foreach ($matches as $val) {
    echo "matched: " . $val[0] . "\n";
    echo "part 1: " . $val[1] . "\n";
    echo "part 2: " . $val[3] . "\n";
    echo "part 3: " . $val[4] . "\n\n";
}
?>

 

See?

Link to comment
Share on other sites

Great Response!  Thank You. 

 

What you are saying is that when

 

<code>

 

preg_match_all("/(<([\w]+)[^>]*>)(.*)(<\/\\2>)/", $html, $matches, PREG_SET_ORDER);

 

</code>

 

preg_match_all is called, $matches is a NULL reference, also $var[x] in the loop is a NULL reference if it wasn't set by preg_match_all. 

 

 

Is this correct?

Link to comment
Share on other sites

This is a OOP forum, and that isn't a oop question, also passing by reference is deprecate and will throw errors on some servers since its been turned off in the php ini file.

 

call-time pass-by-reference has been depreciated. Passing and returning by reference is not likely to be depreciated, ever.

 

I.e.

 

depreciated:

 

function some_function($var){
   return ++$var;
}
$var = some_function(&$otherVar);

 

NOT depreciated:

 

function some_function(&$var){
   return ++$var;
}
$var = some_function($otherVar);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.