[pmwiki-users] Irreproducible behavior - maybe a bug

Joachim Durchholz jo at durchholz.org
Wed May 11 23:57:58 CDT 2005


Patrick R. Michaud wrote:
> Hmm, on my system:
> 
>     <?php
>     
>        header("Content-type: text/plain");
>        print_r($MarkupTable[$id]['dep']);
>        print_r((array)$MarkupTable[$id]['dep']);
>        print_r((array)($MarkupTable[$id]['dep']));
>     
>        var_dump($MarkupTable[$id]['dep']);
>        var_dump((array)$MarkupTable[$id]['dep']);
>        var_dump((array)($MarkupTable[$id]['dep']));
>     ?>
> 
> produces
> 
>     Array
>     (
>     )
>     Array
>     (
>     )
>     NULL
>     array(0) {
>     }
>     array(0) {
>     }
> 
> which is pretty much what I would expect.  It certainly
> doesn't show any elements in the (array) versions...
> (See http://www.pmichaud.com/sandbox/null.php)

I tested my code (pmwiki with heavy var_dump lacing) on another system, 
and got these results (assuming you're testing on 4.3.2, as pmwiki.org 
with ?action=phpinfo suggests):

Server   PHP    Bug?
#1 (Jo)  4.1.2  Present
#2 (Pm)  4.3.2  Not present
#3 (Jo)  4.3.4  Not present

It seems that it's a PHP bug that was fixed somewhere between 4.1.2 and 
4.3.2.

>> For this reason, I proposed to avoid the (array) construct and
>> replace it with the to_array function. That it returns an empty
>> array is just what I think Patrick wanted the (array) type cast to
>> do if the parameter value is not an array (typically, it would be a
>> NULL).
> 
> In every case where (array) is used I've wanted it to convert a
> single non-array value into an array containing that value.

Just for the record: In Markup(), it's used to convert an empty value 
into an empty array, not into a single-element array. IOW PmWiki is 
using the full semantics of (array) as documented on php.net.

So the right way to replace (array) with a PHP function would be:

function to_array($a) {
   if (is_array($a))
     return $a;
   elseif (isset($a))
     return array($a)
   else
     return array();
}

 >> Note that PHP doesn't even have an array() function :-)
 >
 > http://us3.php.net/manual/en/function.array.php

Argh... blame that on php.net. Searching for array just gave me the page 
on arrays, not the array() constructor.

Regards,
Jo



More information about the pmwiki-users mailing list