Strange action with =&
am 13.02.2007 02:29:18 von Peter Beckman
I'm looping through an array and I did this:
$rate =& $mydata[$prefix];
Now, in some cases $mydata[$prefix] wasn't set/defined, so I expected $rate
to not be defined, or at least point to something that wasn't defined.
Instead, PHP 5.1.6 set $mydata[$prefix] to nothing.
If I had:
$mydata[1] = 3;
$mydata[3] = 2;
$mydata[5] = 1;
And did a loop from $i=1; $i++; $i<=5 I'd get:
$mydata[1] = 3;
$mydata[2] = ;
$mydata[3] = 2;
$mydata[4] = ;
$mydata[5] = 1;
Is this expected? A bug? Fixed in 5.2.0? I know I shouldn't set a
reference to a variable that doesn't exist, but the expected result is a
warning/error, not for PHP to populate an array.
Beckman
------------------------------------------------------------ ---------------
Peter Beckman Internet Guy
beckman@purplecow.com http://www.purplecow.com/
------------------------------------------------------------ ---------------
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Strange action with =&
am 13.02.2007 05:39:56 von bedul
sry i don't get what u mean??
----- Original Message -----
From: "Peter Beckman"
To: "PHP-DB Mailing List"
Sent: Tuesday, February 13, 2007 8:29 AM
Subject: [PHP-DB] Strange action with =&
> I'm looping through an array and I did this:
>
> $rate =& $mydata[$prefix];
>
> Now, in some cases $mydata[$prefix] wasn't set/defined, so I expected
$rate
> to not be defined, or at least point to something that wasn't defined.
>
> Instead, PHP 5.1.6 set $mydata[$prefix] to nothing.
>
> If I had:
>
> $mydata[1] = 3;
> $mydata[3] = 2;
> $mydata[5] = 1;
>
> And did a loop from $i=1; $i++; $i<=5 I'd get:
>
> $mydata[1] = 3;
> $mydata[2] = ;
> $mydata[3] = 2;
> $mydata[4] = ;
> $mydata[5] = 1;
the reason mydata2 empty was because it don't have value in it!!
full source plz
why u don't try this
$txt.="";
foreach($mydata as $nm=>$val){
$txt.="\n- $nm = $val";
$txt2="
\$mydata[$nm] = $val";
}
$txt.="
";
print $txt;
>
> Is this expected? A bug? Fixed in 5.2.0? I know I shouldn't set a
> reference to a variable that doesn't exist, but the expected result is a
> warning/error, not for PHP to populate an array.
we should cross check again.
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Strange action with =&
am 13.02.2007 05:53:51 von Peter Beckman
On Tue, 13 Feb 2007, bedul wrote:
> sry i don't get what u mean??
>
>> I'm looping through an array and I did this:
>>
>> $rate =& $mydata[$prefix];
This is how you assign a variable by reference. $rate should be a
reference to $mydata[$prefix], not a copy. If I change the value of
$rate, the value of $mydata[$prefix] is also changed, and vice versa.
>> Now, in some cases $mydata[$prefix] wasn't set/defined, so I expected
>> $rate to not be defined, or at least point to something that wasn't
>> defined.
>>
>> Instead, PHP 5.1.6 set $mydata[$prefix] to nothing.
>>
>> If I had:
>>
>> $mydata[1] = 3;
>> $mydata[3] = 2;
>> $mydata[5] = 1;
>>
>> And did a loop from $i=1; $i++; $i<=5 I'd get:
>>
>> $mydata[1] = 3;
>> $mydata[2] = ;
>> $mydata[3] = 2;
>> $mydata[4] = ;
>> $mydata[5] = 1;
>
> the reason mydata2 empty was because it don't have value in it!!
>
> full source plz
> why u don't try this
>
> $txt.="
";
> foreach($mydata as $nm=>$val){
> $txt.="\n- $nm = $val";
> $txt2="
\$mydata[$nm] = $val";
> }
> $txt.="
";
>
> print $txt;
Because I'm trying to point out a problem with PHP, where setting a
reference when the other side is undefined or not set, PHP creates a
reference to a previously non-existent array item, just by setting a
reference. I don't think that should happen.
Your code doesn't set anything by reference.
>> Is this expected? A bug? Fixed in 5.2.0? I know I shouldn't set a
>> reference to a variable that doesn't exist, but the expected result is a
>> warning/error, not for PHP to populate an array.
>
> we should cross check again.
I don't know what you mean.
------------------------------------------------------------ ---------------
Peter Beckman Internet Guy
beckman@purplecow.com http://www.purplecow.com/
------------------------------------------------------------ ---------------
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Strange action with =&
am 13.02.2007 10:28:32 von Oskar
Peter Beckman wrote:
> Because I'm trying to point out a problem with PHP, where setting a
> reference when the other side is undefined or not set, PHP creates a
> reference to a previously non-existent array item, just by setting a
> reference. I don't think that should happen.
>
And? what's wrong with that. The reference can be used in the future - I
think thats why it doesnt produce any error message.
ie.
$array=array("1"=>"a","3"=>"c");
$ref=&$array["2"];
$array["2"]="b";
echo($ref);
OKi98
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Strange action with =&
am 20.02.2007 10:18:39 von Haydar TUNA
Hello
This (=&) is used in the variable references. in $ref=&$array["2"] line
$ref variable and $array["2"] variable point at the same address. if you
assign any value to this any variables, both of them will change their
values because echo($ref) line display b on the screen.
--
Haydar TUNA
Republic Of Turkey - Ministry of National Education
Education Technology Department Ankara / TURKEY
Web: http://www.haydartuna.net
"OKi98" wrote in message
news:45D184C0.2060208@centrum.cz...
> Peter Beckman wrote:
>
>> Because I'm trying to point out a problem with PHP, where setting a
>> reference when the other side is undefined or not set, PHP creates a
>> reference to a previously non-existent array item, just by setting a
>> reference. I don't think that should happen.
>>
> And? what's wrong with that. The reference can be used in the future - I
> think thats why it doesnt produce any error message.
> ie.
> $array=array("1"=>"a","3"=>"c");
> $ref=&$array["2"];
> $array["2"]="b";
> echo($ref);
>
>
> OKi98
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php