|
Posted by Dikkie Dik on 09/17/05 23:00
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:
> 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();
>
[Back to original message]
|