danp_canucks Posted February 12, 2008 Share Posted February 12, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/90634-is-this-legal/ Share on other sites More sharing options...
448191 Posted February 12, 2008 Share Posted February 12, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/90634-is-this-legal/#findComment-464634 Share on other sites More sharing options...
danp_canucks Posted February 12, 2008 Author Share Posted February 12, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/90634-is-this-legal/#findComment-464643 Share on other sites More sharing options...
Demonic Posted February 12, 2008 Share Posted February 12, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/90634-is-this-legal/#findComment-465054 Share on other sites More sharing options...
448191 Posted February 12, 2008 Share Posted February 12, 2008 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); Quote Link to comment https://forums.phpfreaks.com/topic/90634-is-this-legal/#findComment-465108 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.