ID:               40610
 User updated by:  Webbed dot Pete at gmail dot com
 Reported By:      Webbed dot Pete at gmail dot com
 Status:           Open
 Bug Type:         Documentation problem
 Operating System: Windows, Linux
 PHP Version:      5.2.1
 New Comment:

Typo on function arrset(). Instead of
    ... return $val;
it should be
    ... return $arr[$key];


Previous Comments:
------------------------------------------------------------------------

[2007-02-24 00:38:43] Webbed dot Pete at gmail dot com

<<<When you pass a non-existent variable by reference of course it HAS
to be created, or what do you think should be referenced?>>>

Funny thing is, passing a reference to a non-existent normal variable
works fine. Only array elements require something to be created.

If we accept this as a requirement, I believe we arrive at the
following set of conclusions:

What we're declarng here is that
a) isset() is not identical to "is created".
b) A php app has no way to discover if a variable is created.
c) Array elements must be treated distinctly from other variables
whenever the number of created keys is important.
d) For array elements, key references must be carried separately from
value references.

Thus, to accomplish the equivalent of "isset() with default," without
causing side effects, can still be accomplished with some pain. 

It requires separate functions for variables, constants and array
elements. I believe the following patterns would be correct:

function varset(&$val,$default='') {
        if (isset($val)) return $val;
        return $default;
}

function defset($str,$default='') {
        if (defined($str)) return constant($str);
        return $default;
}

function arrset(&$arr,$key,$default='') {
        if ( isset($arr) && is_array($arr) && array_key_exists($key,$arr) &&
isset($arr[$key]) ) return $val;
        return $default;
}

Corrections welcome.

Thanks!

------------------------------------------------------------------------

[2007-02-23 23:38:31] [EMAIL PROTECTED]

Reclassified a docu problem.
When you pass a non-existent variable by reference of course it HAS to
be created, or what do you think should be referenced?

------------------------------------------------------------------------

[2007-02-23 22:47:54] Webbed dot Pete at gmail dot com

I have double and triple checked the documentation...
http://us3.php.net/manual/en/language.references.whatdo.php
...tells us that
<<<PHP references allow you to make two variables to refer to the same
content. Meaning, when you do:
<?php
$a =& $b;
?>
it means that $a and $b point to the same content. Note: $a and $b are
completely equal here, that's not $a is pointing to $b or vice versa,
that's $a and $b pointing to the same place.>>>

This works as far as it goes. What's left unsaid is that if $b is a
missing array key, the unset key will be created and the $b array will
grow. 

Neither 'array' nor 'references' documentation specify this behavior.
They don't say whether or not array elements are created willy-nilly
when an array reference exists.

I find nothing in the reference manual that either specifies nor
suggests this behavior is intentional. I do find several user comments
about this bug. It's just never been reported as a bug before.

The impact is that we cannot trust the keys of an array.

If this is declared as missing documentation, we will have to live with
the bug.

I'd prefer to see it eventually fixed; php will be better for it.

------------------------------------------------------------------------

[2007-02-23 20:13:24] [EMAIL PROTECTED]

Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php



------------------------------------------------------------------------

[2007-02-23 19:01:44] Webbed dot Pete at gmail dot com

Even simpler:
 $x=&$aEmpty['wheat'];

Adds element 'wheat' to the array.

This creates a nasty side effect for a couple of valuable and common
functions, which basically extend isset() for default values and so
forth:

function varset(&$val,$default='') {
        if (isset($val)) return $val;
        return $default;
}
function varsettrue(&$val,$default='') {
        if (isset($val) && $val) return $val;
        return $default;
}

$myVal = varset($pref['maxsize'],1000); // set myVal to pref or
default

NOTE: all of the following leave $aEmpty alone. I understand why this
might be the case, yet it still is wrong to break references IMHO, not
least because of losing ability to create functions like those above.
  $aEmpty['wheat']; // simple reference
  isset($aEmpty['wheat']); // built-in function
  myFunc($aEmpty['wheat']); // pass-by-value to user func

------------------------------------------------------------------------

The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
    http://bugs.php.net/40610

-- 
Edit this bug report at http://bugs.php.net/?id=40610&edit=1

Reply via email to