Nullifying pointers in PHP

Using pointers in PHP is quite useful at times; but it can also leave you dumbstruck with serious debugging on your hands. Consider this construct:

[code]for ($i = 0; $i < 10; $i++) { $p1 = null; if ($Buffer [$i]['type'] == 'scanthis']) { $p1 =& $Buffer [$i]['data']; } .. if ($p1 != null) .. }//for[/code]
This will in effect dereference and overwrite a given [‘data’] array member of $Buffer if [‘scanthis’] evaluates to true. But I still want to use “null” as an indicator to whether or not to do something.

So I replace the above with:

[code]for ($i = 0; $i < 10; $i++) { unset ($p1); <-- $p1 = null; if ($Buffer [$i]['type'] == 'scanthis']) { $p1 =& $Buffer [$i]['data']; } .. if ($p1 != null) .. }//for [/code]
This will destroy $p1, but not what it’s pointing to, and then it sets $p1 to null, just like before.

This is obvious when you look at it, and there are a number of other ways to accomplish what I describe above, but it had me stumped for a while 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

Notify me of followup comments via e-mail. You can also subscribe without commenting.