473,396 Members | 1,966 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

multidimensional array declaration in php

how to achieve that? it seems php doesn't support it well for a C
programmer?

i hope to use something like:

a[$i][0];

a[$i][1];

a[$i][2];
Jul 15 '06 #1
21 22881
vito wrote:
how to achieve that? it seems php doesn't support it well for a C
programmer?

i hope to use something like:

a[$i][0];

a[$i][1];

a[$i][2];

The only problem i see is it should be

$a[$i][0];
$a[$i][1];
$a[$i][2];

Works as long as $a and $a[$i] are defined as arrays.

$a = array();
$a[$i] = array();
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jul 15 '06 #2
$a[$i] = array();
Is the above declaration legitimate?

Furthermore,
$array[$i][1] = $array[$i][1] . $line;

PHP Notice: Undefined offset: 2700 in array.php on line 16

Jul 15 '06 #3
vito wrote:
> $a[$i] = array();

Is the above declaration legitimate?
as long as $a is defined as an array, yes.
Furthermore,
$array[$i][1] = $array[$i][1] . $line;

PHP Notice: Undefined offset: 2700 in array.php on line 16

Looks like $i contains 2700. Have you initialized this element?
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jul 15 '06 #4
>> $a[$i] = array();
>Is the above declaration legitimate?

as long as $a is defined as an array, yes.
$array = array();

for ($k = 0; $k < 10000; $k++)
$array[$k] = array();

for ($j = 0; $j < 10000; $j++)
for ($k = 0; $k < 3; $k++)
$array[$j][$k] = 0;

How to initialize?

$array[$i][1] = $array[$i][1] . $line;
PHP Notice: Undefined offset: 2700 in array.php on line 24
Jul 15 '06 #5
vito wrote:
>>>$a[$i] = array();

Is the above declaration legitimate?

as long as $a is defined as an array, yes.


$array = array();

for ($k = 0; $k < 10000; $k++)
$array[$k] = array();

for ($j = 0; $j < 10000; $j++)
for ($k = 0; $k < 3; $k++)
$array[$j][$k] = 0;

How to initialize?

$array[$i][1] = $array[$i][1] . $line;
PHP Notice: Undefined offset: 2700 in array.php on line 24

Well, in this case you're trying to initialize 10,000 * 10,000 arrays,
each with an integer. On a 32 bit system that's something like
400,000,000 bytes (close to 400 mb) of storage plus the overhead of the
arrays themselves. Probably a bit more than you have available - the
default is around 8M.

Why would you ever try to initialize this much memory (in either PHP or
C/C++) anyway?

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jul 15 '06 #6
Well, in this case you're trying to initialize 10,000 * 10,000 arrays,
each with an integer. On a 32 bit system that's something like
400,000,000 bytes (close to 400 mb) of storage plus the overhead of the
arrays themselves. Probably a bit more than you have available - the
default is around 8M.

Why would you ever try to initialize this much memory (in either PHP or
C/C++) anyway?
Indeed, i just hope to make an array of a[10000][3] and then can be used
later. i'd be glad if you could tell me how to initialize such an array.
Thanks.
Jul 15 '06 #7
vito wrote:
>>Well, in this case you're trying to initialize 10,000 * 10,000 arrays,
each with an integer. On a 32 bit system that's something like
400,000,000 bytes (close to 400 mb) of storage plus the overhead of the
arrays themselves. Probably a bit more than you have available - the
default is around 8M.

Why would you ever try to initialize this much memory (in either PHP or
C/C++) anyway?


Indeed, i just hope to make an array of a[10000][3] and then can be used
later. i'd be glad if you could tell me how to initialize such an array.
Thanks.

OK, here's a good place to start with arrays:

http://us3.php.net/manual/en/language.types.array.php

However, I still fail to see why you need to define 30K elements.
That's an awful lot for a PHP routine. But you know more about what
you're doing.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jul 16 '06 #8

Hi vito, you can think of php arrays as being dynamic -- you don't have
to declare it's size ahead of time like in C or Java. I'm pretty sure
they dynamically resize themselves to fit their contents.

So you would just:

for (...) {
$newArray = array(1,2,3)
$myarray[] = $newArray;
}

But 10,000 can be very big; you should think about your problem
solution.

vito wrote:
how to achieve that? it seems php doesn't support it well for a C
programmer?

i hope to use something like:

a[$i][0];

a[$i][1];

a[$i][2];
Jul 16 '06 #9
Jerry Stuckle wrote:
vito wrote:
[snip]
>Indeed, i just hope to make an array of a[10000][3] and then can be used
later. i'd be glad if you could tell me how to initialize such an array.
Thanks.
vito,
Your code should do the trick, that is making an array of 10000 elements,
each containing an array of 3 elements, each set to integer value zero, if
that is the intention. Can't say why you get the notice. 2700 shouldn't be
an undefined offset.

Notice that Jerry keep using the word *define*. Should probably be
emphasized since you're a C-programmer. There is no explicit declaration of
a variable like you would do in C, as it is just a generic storage-place
for a value, which content can be defined or not.
Even if not defined, a legal expression of an lvalue can be assigned a
value. PHP will happily create/extend arrays or objects if needed. Used as
a rvalue (like in your case) you will get a notice of this, but it can
safely be ignored (or suppressed with @) if the use is intended.

PHP will also quite happily coerce pretty much anything into something else.
E.g. you give all $array[$j][$k] the integer value 0, but later concatenate
$array[$i][1] with something else as if a string. Sometimes such automatic
typecasting makes sense, sometimes not.
The really good thing about it, is that it lets you write rather silly
things that actually works. Really takes the r out of boring.
Just for the point:
$array= array('is what we need').'_';
$indeed=$array.= @${', Hong Kong, we got an array to'}.'fill';
$array= $indeed("of","10000 rows. Each an",$array('with',3,@${'cats'}));

....which really just is:

$array = array_fill(0, 10000, array_fill(0, 3, null));
OK, here's a good place to start with arrays:

http://us3.php.net/manual/en/language.types.array.php

However, I still fail to see why you need to define 30K elements.
That's an awful lot for a PHP routine. But you know more about what
you're doing.
Well, yeah, sometimes one just find the urge to try something and see how it
goes. Perhaps that is vito's deal.
I once tried using php to traverse 3 Mill. records from a db. Nothing fancy
I thought, just one at the time, do a "little" fiddling and occasionally
throw a chunk someplace else. Needless to say perhaps, I ended up doing it
in C instead.

--
/Bent
Jul 16 '06 #10
Bent Stigsen wrote:
Jerry Stuckle wrote:
>>vito wrote:

[snip]
>>>Indeed, i just hope to make an array of a[10000][3] and then can be used
later. i'd be glad if you could tell me how to initialize such an array.
Thanks.


vito,
Your code should do the trick, that is making an array of 10000 elements,
each containing an array of 3 elements, each set to integer value zero, if
that is the intention. Can't say why you get the notice. 2700 shouldn't be
an undefined offset.

Notice that Jerry keep using the word *define*. Should probably be
emphasized since you're a C-programmer. There is no explicit declaration of
a variable like you would do in C, as it is just a generic storage-place
for a value, which content can be defined or not.
Even if not defined, a legal expression of an lvalue can be assigned a
value. PHP will happily create/extend arrays or objects if needed. Used as
a rvalue (like in your case) you will get a notice of this, but it can
safely be ignored (or suppressed with @) if the use is intended.
True, I'm not using it in the C sense. But I'm not using "define" in
the Cobol sense, either :-). In PHP, assigning a value to a variable
defines it. And it can't be used anyplace other then the left side of
an assignment operator (rvalue in C terms) until it is defined.

However, most C programmers aren't aware of the terms lvalue and rvalue,
either, or if they are, have only a hazy idea of what they mean.
PHP will also quite happily coerce pretty much anything into something else.
E.g. you give all $array[$j][$k] the integer value 0, but later concatenate
$array[$i][1] with something else as if a string. Sometimes such automatic
typecasting makes sense, sometimes not.
The really good thing about it, is that it lets you write rather silly
things that actually works. Really takes the r out of boring.
Just for the point:
$array= array('is what we need').'_';
$indeed=$array.= @${', Hong Kong, we got an array to'}.'fill';
$array= $indeed("of","10000 rows. Each an",$array('with',3,@${'cats'}));

...which really just is:

$array = array_fill(0, 10000, array_fill(0, 3, null));

>>OK, here's a good place to start with arrays:

http://us3.php.net/manual/en/language.types.array.php

However, I still fail to see why you need to define 30K elements.
That's an awful lot for a PHP routine. But you know more about what
you're doing.


Well, yeah, sometimes one just find the urge to try something and see how it
goes. Perhaps that is vito's deal.
I once tried using php to traverse 3 Mill. records from a db. Nothing fancy
I thought, just one at the time, do a "little" fiddling and occasionally
throw a chunk someplace else. Needless to say perhaps, I ended up doing it
in C instead.
That's true - I've gone through some awfully large files and databases
myself. But I've never had to create an array of 30K items in PHP, either.

Now in C/C++ I have done this. But these programs are typically much
more complex than PHP programs. For instance - I haven't seen too many
500K LOC PHP programs. But I have worked on several over the years in
C/C++.

My point being - PHP is a different language - and one should be
approaching PHP development differently than C/C++ development.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jul 16 '06 #11
Jerry Stuckle wrote:
Bent Stigsen wrote:
[snip]
True, I'm not using it in the C sense. But I'm not using "define" in
the Cobol sense, either :-). In PHP, assigning a value to a variable
defines it.
That sortof went over my head. Never done Cobol. :)
I didn't really consider the subtler differences. Not sure I understand
PHP's way anyway. For instance:

unset($a);
$b = null;

"isset" returns false for both $a and $b, even though key of 'b' is present
in the $GLOBALS array.
Use of $a will generate a notice, but not when using $b.

I don't understand the point in that.

And it can't be used anyplace other then the left side of
an assignment operator (rvalue in C terms) until it is defined.
Not sure I follow. I think it would depend on what the intention and
situation. In vito's case, there shouldn't be a problem using it
($array[$i][1]) as an rvalue when undefined if just used to concatenate
strings, as it will initially just appear as an empty string. Just
suppressing the notice should work fine.
I.e.: $array[$i][1] = @$array[$i][1] . $line;

In other cases it, then sure it would be an error.

However, most C programmers aren't aware of the terms lvalue and rvalue,
either, or if they are, have only a hazy idea of what they mean.
Would they not know? I believe the compiler will use the terms if one screws
up, allthough perhaps one should be drunk to cause it.
[snip]
Now in C/C++ I have done this. But these programs are typically much
more complex than PHP programs. For instance - I haven't seen too many
500K LOC PHP programs. But I have worked on several over the years in
C/C++.
Can be generated in seconds, no biggie :)

awk 'BEGIN{print"<?php";for(i=0;i<ARGV[1];i++){printf"echo\"";
n=int(35*(1+sin(i/10)));for(j=0;j<int(n/8);j++)printf"\t";
for(j=0;j<n%8;j++)printf"\040";printf("%d\\n\";\n" ,i)}
print"?>";}' 500001 bigbadacode.php

Bit of a memory-hog and pretty useless though, but it exceeds 500K.

My point being - PHP is a different language - and one should be
approaching PHP development differently than C/C++ development.
For sure. My oppinion of PHP varies a lot. One day I would consider it a
rotting stinking goat carcass which only dogs would take a roll in
(figuratively speaking, no offense to dogs), find myself rolling in it the
very next day, and cant stand the smell on the third day.

--
/Bent
Jul 17 '06 #12

"Bent Stigsen" <ng**@thevoid.dkwrote in message
news:45****************@thevoid.dk...
Jerry Stuckle wrote:
>Bent Stigsen wrote:
[snip]
>True, I'm not using it in the C sense. But I'm not using "define" in
the Cobol sense, either :-). In PHP, assigning a value to a variable
defines it.

That sortof went over my head. Never done Cobol. :)
I didn't really consider the subtler differences. Not sure I understand
PHP's way anyway. For instance:

unset($a);
$b = null;

"isset" returns false for both $a and $b, even though key of 'b' is
present
in the $GLOBALS array.
Use of $a will generate a notice, but not when using $b.

I don't understand the point in that.
You can put a value into a variable without having to define the variable
beforehand. If you attempt to read from a variable that does not yet exist
then a ntice is generated. What's so difficult about that?
>
>And it can't be used anyplace other then the left side of
an assignment operator (rvalue in C terms) until it is defined.

Not sure I follow.
If it's on the left you are putting a value in, if it's on the right you are
trying to get a value out.
I think it would depend on what the intention and
situation. In vito's case, there shouldn't be a problem using it
($array[$i][1]) as an rvalue when undefined if just used to concatenate
strings, as it will initially just appear as an empty string. Just
suppressing the notice should work fine.
I.e.: $array[$i][1] = @$array[$i][1] . $line;

In other cases it, then sure it would be an error.

>However, most C programmers aren't aware of the terms lvalue and rvalue,
either, or if they are, have only a hazy idea of what they mean.

Would they not know? I believe the compiler will use the terms if one
screws
up, allthough perhaps one should be drunk to cause it.
[snip]
>Now in C/C++ I have done this. But these programs are typically much
more complex than PHP programs. For instance - I haven't seen too many
500K LOC PHP programs. But I have worked on several over the years in
C/C++.

Can be generated in seconds, no biggie :)

awk 'BEGIN{print"<?php";for(i=0;i<ARGV[1];i++){printf"echo\"";
n=int(35*(1+sin(i/10)));for(j=0;j<int(n/8);j++)printf"\t";
for(j=0;j<n%8;j++)printf"\040";printf("%d\\n\";\n" ,i)}
print"?>";}' 500001 bigbadacode.php

Bit of a memory-hog and pretty useless though, but it exceeds 500K.

>My point being - PHP is a different language - and one should be
approaching PHP development differently than C/C++ development.

For sure. My oppinion of PHP varies a lot. One day I would consider it a
rotting stinking goat carcass which only dogs would take a roll in
(figuratively speaking, no offense to dogs), find myself rolling in it the
very next day, and cant stand the smell on the third day.
This tells me that you are either using he language incorrectly, or you are
a pretty poor programmer.

--
Tony Marston
http://www.tonymarston.net
http://www.radicore.org
Jul 17 '06 #13
Bent Stigsen wrote:
Jerry Stuckle wrote:
>>Bent Stigsen wrote:

[snip]
>>True, I'm not using it in the C sense. But I'm not using "define" in
the Cobol sense, either :-). In PHP, assigning a value to a variable
defines it.


That sortof went over my head. Never done Cobol. :)
I didn't really consider the subtler differences. Not sure I understand
PHP's way anyway. For instance:

unset($a);
$b = null;

"isset" returns false for both $a and $b, even though key of 'b' is present
in the $GLOBALS array.
Use of $a will generate a notice, but not when using $b.

I don't understand the point in that.
unset() allows the garbage collector to completely clear all references
to $a, while $b is still defined. So the former would release some
memory, while the latter just indicates the variable has no value.

Now - I agree with you that there may not be a lot of point to the
difference. Normally I don't use unset() for individual variables, but
I will use it for large arrays.
>
>>And it can't be used anyplace other then the left side of
an assignment operator (rvalue in C terms) until it is defined.


Not sure I follow. I think it would depend on what the intention and
situation. In vito's case, there shouldn't be a problem using it
($array[$i][1]) as an rvalue when undefined if just used to concatenate
strings, as it will initially just appear as an empty string. Just
suppressing the notice should work fine.
I.e.: $array[$i][1] = @$array[$i][1] . $line;

In other cases it, then sure it would be an error.
By default an undefined variable can never be an rvalue. It will not
appear as an empty string - it will generate an error.

Using the @ just hides the error message. It doesn't stop the error
from occurring, and is, IMHO, VERY BAD practice.

Now - if the variable were defined as it should have been, there would
be no error.
>
>>However, most C programmers aren't aware of the terms lvalue and rvalue,
either, or if they are, have only a hazy idea of what they mean.


Would they not know? I believe the compiler will use the terms if one screws
up, allthough perhaps one should be drunk to cause it.
Depending on the error they get, yes. Very few errors mention lvalue or
rvalue, and those are not common errors. And even if the error message
indicates "lvalue" or "rvalue", the programmers don't necessarily know
what the terms mean.
>
[snip]
>>Now in C/C++ I have done this. But these programs are typically much
more complex than PHP programs. For instance - I haven't seen too many
500K LOC PHP programs. But I have worked on several over the years in
C/C++.


Can be generated in seconds, no biggie :)
I'm not talking about programs generated by an inefficient code
generator. I'm talking about 500K+ LOC projects generated by hand by
dozens of experienced programmers over a period of a year or more.

Code that actually works and does something useful.
awk 'BEGIN{print"<?php";for(i=0;i<ARGV[1];i++){printf"echo\"";
n=int(35*(1+sin(i/10)));for(j=0;j<int(n/8);j++)printf"\t";
for(j=0;j<n%8;j++)printf"\040";printf("%d\\n\";\n" ,i)}
print"?>";}' 500001 bigbadacode.php

Bit of a memory-hog and pretty useless though, but it exceeds 500K.
>>My point being - PHP is a different language - and one should be
approaching PHP development differently than C/C++ development.


For sure. My oppinion of PHP varies a lot. One day I would consider it a
rotting stinking goat carcass which only dogs would take a roll in
(figuratively speaking, no offense to dogs), find myself rolling in it the
very next day, and cant stand the smell on the third day.
I think it's a decent language for what it's designed. Not great, not
bad. But highly useful and improving.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jul 17 '06 #14
Tony Marston wrote:
"Bent Stigsen" <ng**@thevoid.dkwrote in message
news:45****************@thevoid.dk...
>Jerry Stuckle wrote:
>>Bent Stigsen wrote:
[snip]
>>True, I'm not using it in the C sense. But I'm not using "define" in
the Cobol sense, either :-). In PHP, assigning a value to a variable
defines it.
That sortof went over my head. Never done Cobol. :)
I didn't really consider the subtler differences. Not sure I understand
PHP's way anyway. For instance:

unset($a);
$b = null;

"isset" returns false for both $a and $b, even though key of 'b' is
present
in the $GLOBALS array.
Use of $a will generate a notice, but not when using $b.

I don't understand the point in that.

You can put a value into a variable without having to define the variable
beforehand. If you attempt to read from a variable that does not yet exist
then a ntice is generated. What's so difficult about that?
It's quite clearly stated in the manual, no problem there. I just
don't understand the choice of behavior.

It is the concept of "isset", which I like to take literally.
When I set the variable to null, causing the variable to appear in
$GLOBALS, why should "isset" return false?

I understand the behavior, not the choice.

>>And it can't be used anyplace other then the left side of
an assignment operator (rvalue in C terms) until it is defined.
Not sure I follow.

If it's on the left you are putting a value in, if it's on the right you are
trying to get a value out.
I gathered that was a typo. Pretty hard to mix up left and right of
Lvalues and Rvalues.
[snip]
>>My point being - PHP is a different language - and one should be
approaching PHP development differently than C/C++ development.
For sure. My oppinion of PHP varies a lot. One day I would consider it a
rotting stinking goat carcass which only dogs would take a roll in
(figuratively speaking, no offense to dogs), find myself rolling in it the
very next day, and cant stand the smell on the third day.

This tells me that you are either using he language incorrectly, or you are
a pretty poor programmer.
Silly, how would you know how pretty or poor I am?

I don't know if I is incorrect use that flavours my opinion. Hardly a
way to solve the lack of speed. It just isn't for number crunching.
Scope-rules usually annoys me. Behavior of isset bugs me.
This bugs me:
$a = array(1,2,3);
foreach($a as &$val) $val = $val * 2;
foreach($a as $key=>$val) echo "$key = $val\n";
Returns:
0 = 2
1 = 4
2 = 4
Behavior by design, no problem, but I hate when I have to read the
fine print in the manual, if I on top of that find it illogical, then
it is a stinking pile of cow dung.

--
/Bent
Jul 17 '06 #15
ybakos schrieb:
>>
i hope to use something like:

a[$i][0];

a[$i][1];

a[$i][2];
[...]
for (...) {
$newArray = array(1,2,3)
$myarray[] = $newArray;
}
As Arrays are passed by value and not by reference, you can define the
new array outside the for loop.

$myArray = array();
$newArray = array(0, 0, 0);
for ($i=0; $i<10000; $i++) {
$myArray[] = $newArray;
}

or

$myArray = array();
for ($i=0; $i<10000; $i++) {
$myArray[] = array(0, 0, 0);
}

I don't know which is faster.
Anyway this does not explain the undefined offset notice, and it does
not really answer why one should do this at all :-)

--
Markus
Jul 17 '06 #16
Bent Stigsen wrote:
Tony Marston wrote:
>>
You can put a value into a variable without having to define the
variable beforehand. If you attempt to read from a variable that does
not yet exist then a ntice is generated. What's so difficult about that?


It's quite clearly stated in the manual, no problem there. I just don't
understand the choice of behavior.

It is the concept of "isset", which I like to take literally.
When I set the variable to null, causing the variable to appear in
$GLOBALS, why should "isset" return false?

I understand the behavior, not the choice.
Because null is not a value. It is specifically the *lack of a value*.
Therefore, the variable does not contain a value - it is not set.
>
I don't know if I is incorrect use that flavours my opinion. Hardly a
way to solve the lack of speed. It just isn't for number crunching.
Scope-rules usually annoys me. Behavior of isset bugs me.
This bugs me:
$a = array(1,2,3);
foreach($a as &$val) $val = $val * 2;
foreach($a as $key=>$val) echo "$key = $val\n";
Returns:
0 = 2
1 = 4
2 = 4
Behavior by design, no problem, but I hate when I have to read the fine
print in the manual, if I on top of that find it illogical, then it is a
stinking pile of cow dung.
What's illogical about it? In order to get a value out of a variable,
you need to have put a value into that variable. Nothing illogical there.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jul 17 '06 #17
Markus Ernst wrote:
As Arrays are passed by value and not by reference, you can define the
new array outside the for loop.

$myArray = array();
$newArray = array(0, 0, 0);
for ($i=0; $i<10000; $i++) {
$myArray[] = $newArray;
}

or

$myArray = array();
for ($i=0; $i<10000; $i++) {
$myArray[] = array(0, 0, 0);
}

I don't know which is faster.
Anyway this does not explain the undefined offset notice, and it does
not really answer why one should do this at all :-)
The first one is faster, since a single array is created vs. 10000. It
also uses a whole lot less memory.

Jul 17 '06 #18
Jerry Stuckle wrote:
Bent Stigsen wrote:
[snip]
>I don't know if I is incorrect use that flavours my opinion. Hardly a
way to solve the lack of speed. It just isn't for number crunching.
Scope-rules usually annoys me. Behavior of isset bugs me.
This bugs me:
$a = array(1,2,3);
foreach($a as &$val) $val = $val * 2;
foreach($a as $key=>$val) echo "$key = $val\n";
Returns:
0 = 2
1 = 4
2 = 4
Behavior by design, no problem, but I hate when I have to read the
fine print in the manual, if I on top of that find it illogical, then
it is a stinking pile of cow dung.

What's illogical about it? In order to get a value out of a variable,
you need to have put a value into that variable. Nothing illogical there.
echo $a; // Notice generated: "Notice: Undefined variable: a in ..."
$a = null;
echo $a; // No notice is generated

This makes good sense to me, since I did define it to "something",
whatever the concept of null is.

When it comes to "being set", I don't find it particularly neat. It
lacks symmetry.
If "isset" returns true, then "unset" has an effect on the system, but
if "isset" returns false, well then "unset" might have an effect, it
depends.
Not a problem, just ugly in my opinion.

--
/Bent
Jul 17 '06 #19
Bent Stigsen wrote:
It's quite clearly stated in the manual, no problem there. I just
don't understand the choice of behavior.

It is the concept of "isset", which I like to take literally.
When I set the variable to null, causing the variable to appear in
$GLOBALS, why should "isset" return false?

I understand the behavior, not the choice.
Part of the reason is the way the global and static keyword was
implemented. These are operators, not declarations. When you use them
on variables they create entries in the current symbol table. Thus if
isset() behaves as you described, it'll always return true in the
following:

<?

function bobo() {
static $the_clown;
if(isset($the_clown)) {
}
}

?>

which wouldn't be rather unintuitive, since you never actually set the
variable.

Jul 17 '06 #20
Chung Leong wrote:
Bent Stigsen wrote:
>It's quite clearly stated in the manual, no problem there. I just
don't understand the choice of behavior.

It is the concept of "isset", which I like to take literally.
When I set the variable to null, causing the variable to appear in
$GLOBALS, why should "isset" return false?

I understand the behavior, not the choice.

Part of the reason is the way the global and static keyword was
implemented. These are operators, not declarations. When you use them
on variables they create entries in the current symbol table. Thus if
isset() behaves as you described, it'll always return true in the
following:

<?

function bobo() {
static $the_clown;
if(isset($the_clown)) {
}
}

?>

which wouldn't be rather unintuitive, since you never actually set the
variable.
Yes, I guess it would, but I would say creating them in the first
place is unintuitive too. At least for global.
e.g.:

<?php
echo $a; //generates error, $a not in $GLOBALS
foo();
echo $a; //no error, $a is now in $GLOBALS

function foo() {
global $a;
echo $a; //no error
}
?>

It is not that I in a tight spot haven't made that kind of choice of
getting a feature at the cost of a less sensible behavior, in order to
avoid the need to rewrite a lot of code. But personally I would rather
just (temporarily) accept the odd effect of the modifiers, rather than
seeing other functions being adapted to it, if that is the case.

--
/Bent
Jul 18 '06 #21
Bent Stigsen wrote:
It is not that I in a tight spot haven't made that kind of choice of
getting a feature at the cost of a less sensible behavior, in order to
avoid the need to rewrite a lot of code. But personally I would rather
just (temporarily) accept the odd effect of the modifiers, rather than
seeing other functions being adapted to it, if that is the case.
We're not looking at just a couple similiar of odd cases though.
Similiar logic pervades the language engine. Uninitialized object
properties for example. Or when you make a reference to an array
element that doesn't yet exist. Basically anytime PHP needs a blank
placeholder it sets it to null. In order to make isset() sematically
correct, you'd have to either create a new "uninitialized" type or make
null a dual-state type. Since the difference is so slight, it's really
hard to do.

The way I see it, isset()'s raison d'etre is register_globals. That's
the only sensible scenario where a programmer cannot control whether a
variable exists or not. Since register_globals is deprecated, isset()
is sort of obsolete.

Jul 18 '06 #22

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Mark Smith | last post by:
I'm trying to copy data from a 1D array to a 2D array. The obvious thing doesn't work: int twoDee = new int; int oneDee = new int { 1, 2 }; Array.Copy(oneDee, 2, twoDee, 2, 2); This causes a...
2
by: chris | last post by:
Hi there, I created a Multidimensional array of labels Label lblMultiArray = new Label { {Label3, LblThuTotal}, {Label4,LblFriTotal} }; Now I would like to compare the values in the array,...
10
by: | last post by:
I'm fairly new to ASP and must admit its proving a lot more unnecessarily complicated than the other languages I know. I feel this is because there aren't many good official resources out there to...
4
by: Gregory.A.Book | last post by:
I'm working with displaying and manipulating very large image sets. The program handles anything from 2D images to 4D RGB volumes in a time-series. I've been using dynamically allocated arrays to...
5
by: LittleCake | last post by:
Hi All, I have a multidimensional array where each sub-array contains just two entries, which indicates a relationship between those two entries. for example the first sub-array: =Array ( =30...
1
by: shailajaAdiga | last post by:
Hi All, there are 4 different categories which each month will bw updated. In each category(source),there are many editions. I have to display 6months updates. its like one is month array which...
1
by: nivaz | last post by:
hai friends plz help me in this matter, i want to know really about the declaration of multidimensional array in VB. plz do reply
2
by: ...vagrahb | last post by:
I am having accessing individual rows from a multidimensional array pass to a function as reference CODE: function Declaration int Part_Buffer(char (*buffer),int Low, int High)
2
Brandenburg
by: Brandenburg | last post by:
I'm writing a game in visual basic 2008 and things are going fairly well with it for the most part.. well until today.. I can declare a 1 dimensional array of a class just fine but cannot for the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.