Connecting Tech Pros Worldwide Forums | Help | Site Map

undefined array index question

petermichaux@yahoo.com
Guest
 
Posts: n/a
#1: Sep 17 '05
Hi,

I am curious about how php deals with the following situation where I
use an undefined index into an array. PHP seems to be behaving exactly
how I want it to but I want to make sure that it is not a fluke. It
seems like most programming languages would crash if you used an
undefined index. Why does PHP work the way it does?

My example is below

Thanks,
Peter

<?php

$array['foo']=3;
echo 'M'.$array['foo'].'M';
//How can I index into the array to an index that doesn't exist?
echo 'M'.$array['bar'].'M';

//output
//M3MMM
//note there is no space between the last two M's
//this is exactly the result I want but why does it work?
?>


Oli Filth
Guest
 
Posts: n/a
#2: Sep 17 '05

re: undefined array index question


petermichaux@yahoo.com said the following on 17/09/2005 17:47:[color=blue]
> Hi,
>
> I am curious about how php deals with the following situation where I
> use an undefined index into an array. PHP seems to be behaving exactly
> how I want it to but I want to make sure that it is not a fluke. It
> seems like most programming languages would crash if you used an
> undefined index. Why does PHP work the way it does?
>
> My example is below
>
> Thanks,
> Peter
>
> <?php
>
> $array['foo']=3;
> echo 'M'.$array['foo'].'M';
> //How can I index into the array to an index that doesn't exist?
> echo 'M'.$array['bar'].'M';
>
> //output
> //M3MMM
> //note there is no space between the last two M's
> //this is exactly the result I want but why does it work?
> ?>
>[/color]

The only reason nothing breaks is because you must have PHP
error-reporing disabled.

If you had error-reporting enabled, you'd see a message like:

Notice: Undefined index: bar in D:\htdocs\Test\08.php on line 5

--
Oli
petermichaux@yahoo.com
Guest
 
Posts: n/a
#3: Sep 17 '05

re: undefined array index question


How do I enable error reporting?

Thank,
Peter

petermichaux@yahoo.com
Guest
 
Posts: n/a
#4: Sep 17 '05

re: undefined array index question


Oops. I have figured out how to test this problem with error-reporting
enabled.

However it looks like I over simplified my problem and my question
still remains. Here is an improved version of my question. In the
following code there are no errors reported. However, if I uncomment
the one comment then I will get an error "Undefined index: bar ".

Any ideas why I can use the undefined index 'bar' in $this->mArray as
long as $this->mArray has never been initiated/used?

Thanks again,
Peter

class MyClass
{
private $mArray;

public function PrintArrayElement()
{
//$this->mArray['foo']=3;
echo $this->mArray['bar'];
}
}

$my_class = new MyClass;
$my_class->PrintArrayElement();

Dikkie Dik
Guest
 
Posts: n/a
#5: Sep 17 '05

re: undefined array index question


That is because PHP casts variables as needed. Lots of other programming
languages do this also. This means that you can compute 2 + '3' and get
5, because the string is converted to an integer. I personally hate
this, because computing '3' + 2 gets '32'. Which can be converted in a
later expression to 32!
The non-existing variable is evaluated as NULL with a warning, not a
fatal error. NULL gets cast to an empty string, which is "glued" between
the 'M' strings. If you don't want the warning, you can temporarily
switch it off by putting a '@' character in front of the expression or
the command, like
@$result=mArray['NonExistingKey'];
echo 'M' . $result . 'M';

You can use this feature to check if variables exists in a quick-and
dirty way:
@$strUrlParameter=$_GET['Command'];
if(is_null($strUrlParameter))
...

Best regards

petermichaux@yahoo.com wrote:[color=blue]
> Oops. I have figured out how to test this problem with error-reporting
> enabled.
>
> However it looks like I over simplified my problem and my question
> still remains. Here is an improved version of my question. In the
> following code there are no errors reported. However, if I uncomment
> the one comment then I will get an error "Undefined index: bar ".
>
> Any ideas why I can use the undefined index 'bar' in $this->mArray as
> long as $this->mArray has never been initiated/used?
>
> Thanks again,
> Peter
>
> class MyClass
> {
> private $mArray;
>
> public function PrintArrayElement()
> {
> //$this->mArray['foo']=3;
> echo $this->mArray['bar'];
> }
> }
>
> $my_class = new MyClass;
> $my_class->PrintArrayElement();
>[/color]
ZeldorBlat
Guest
 
Posts: n/a
#6: Sep 17 '05

re: undefined array index question


>You can use this feature to check if variables exists in a quick-and[color=blue]
>dirty way:
>@$strUrlParameter=$_GET['Command'];
>if(is_null($strUrlParameter))[/color]

That is dirty :) Probably a little better like this:

if(isset($_GET['Command')) {
//do something
}

Bent Stigsen
Guest
 
Posts: n/a
#7: Sep 17 '05

re: undefined array index question


Dikkie Dik wrote:
[snip][color=blue]
> The non-existing variable is evaluated as NULL with a warning, not a
> fatal error. NULL gets cast to an empty string, which is "glued" between
> the 'M' strings. If you don't want the warning, you can temporarily
> switch it off by putting a '@' character in front of the expression or
> the command, like
> @$result=mArray['NonExistingKey'];
> echo 'M' . $result . 'M';
>
> You can use this feature to check if variables exists in a quick-and
> dirty way:
> @$strUrlParameter=$_GET['Command'];
> if(is_null($strUrlParameter))
> ...[/color]
[snip]

"isset" can also be used for this, which will suppress warning for
non-existent index. However "isset", or the code ove, will not tell
the difference between, an existing index assigned a NULL-value, and a
non-existing index.
That is probably also the point with accepting a non-existing index as
non-fatal. That is, why enforce a verification of an index, when
validation of its value is just as important. But as it just might be
a mistake, a warning is in order.

/Bent
Dikkie Dik
Guest
 
Posts: n/a
#8: Sep 17 '05

re: undefined array index question


Dikkie Dik wrote:[color=blue]
> ... Lots of other programming
> languages do this also. This means that you can compute 2 + '3' and get
> 5, because the string is converted to an integer. I personally hate
> this, because computing '3' + 2 gets '32'. Which can be converted in a
> later expression to 32![/color]

I was thinking too much about java and Visual Basic. PHP doesn't want to
"add" strings at all, so '3' + 2 gets 5.
Mladen Gogala
Guest
 
Posts: n/a
#9: Sep 18 '05

re: undefined array index question


On Sat, 17 Sep 2005 23:02:20 +0200, Dikkie Dik wrote:
[color=blue]
> I was thinking too much about java and Visual Basic. PHP doesn't want to
> "add" strings at all, so '3' + 2 gets 5.[/color]

try with this:

<?php
$a=3 . 2;
print "$a\n";
?>


--
http://www.mgogala.com

petermichaux@yahoo.com
Guest
 
Posts: n/a
#10: Sep 18 '05

re: undefined array index question


I am trying to get a message something like your Notice message. What
do I need to change about my error handler? I thought my error handler
would catch Errors, Warnings, User Notices and Notices.

Thanks,
Peter

<?php

set_error_handler("my_error_handler", E_ALL);

function my_error_handler($errNo, $errStr, $errFile, $errLine)
{
$error_message = "\nERRNO: ". $errNo ."\nTEXT: " . $errStr . " \n" .
"LOCATION: " . $errFile . ", line " . $errLine . ", at "
..
date("F j, Y, g:i a") . "\n\n";


echo "<pre>" . $error_message . "</pre>";
exit;
}

$array['foo']=3;
echo 'M'.$array['foo'].'M';
//How can I index into the array to an index that doesn't exist?
echo 'M'.$array['bar'].'M';
?>

Closed Thread