469,290 Members | 1,913 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,290 developers. It's quick & easy.

undefined array index question

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?
?>

Sep 17 '05 #1
9 9318
pe**********@yahoo.com said the following on 17/09/2005 17:47:
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?
?>


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
Sep 17 '05 #2
How do I enable error reporting?

Thank,
Peter

Sep 17 '05 #3
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();

Sep 17 '05 #4
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

pe**********@yahoo.com wrote:
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();

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


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

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

Sep 17 '05 #6
Dikkie Dik wrote:
[snip]
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))
...

[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
Sep 17 '05 #7
Dikkie Dik wrote:
... 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!


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


try with this:

<?php
$a=3 . 2;
print "$a\n";
?>
--
http://www.mgogala.com

Sep 17 '05 #9
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';
?>

Sep 18 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by lawrence | last post: by
9 posts views Thread by Alan Schroeder | last post: by
7 posts views Thread by deepak | last post: by
5 posts views Thread by Pseudonyme | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.