RE: global array

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 




Jeff Burcher <jeff@xxxxxxxxxxxxxxx> hat am 14. Juni 2012 um 14:23 geschrieben:

> You're a genius!! Thank you. Uppercase 'R', sheesh. PHP is sooo picky. I
> worked for two days trying to figure that one out. Anyway, for future
> reference, you can pass the entire array as a variable like that?? and do you
> know if the '+=' statement will create an array entry if one doesn't exist?


If you are using a higher loglevel, you'll get a notice for a not existing array
key.
In the othercase

$array[$mykey] += 1;

will work without notice. But as the key does not exist, the value will be null
and right now I am not sure what

null + 1

evaluates to?

Well, works....

maro@marco-behnke:~$ php -a
Interactive shell

php > $array = array();
php > $array['foo'] += 1;
PHP Notice:  Undefined index: foo in php shell code on line 1
PHP Stack trace:
PHP   1. {main}() php shell code:0
php > var_dump($array);
array(1) {
  ["foo"]=>
  int(1)
}

BUT I stronly recommend not to do that.

make it this way:

$array[$mykey] = array_key_exists($mykey, $array) ? $array[$mykey] += 1 :
$array[$mykey] = 1;

or better:

if (array_key_exists($mykey, $array)) {
   $array[$mykey] += 1;
} else {
   $array[$mykey] = 1;
}

>
> Thanks,
>
> Jeff Burcher - IT Dept
> Allred Metal Stamping
> PO Box 2566
> High Point, NC 27261
> (336)886-5221 x229
> jeff@xxxxxxxxxxxxxxx
>
>
> > -----Original Message-----
> > From: marco@xxxxxxxxxx [mailto:marco@xxxxxxxxxx]
> > Sent: Thursday, June 14, 2012 8:04 AM
> > To: php-general@xxxxxxxxxxxxx; jeff@xxxxxxxxxxxxxxx
> > Subject: Re:  global array
> >
> >
> >
> >
> > Jeff Burcher <jeff@xxxxxxxxxxxxxxx> hat am 14. Juni 2012 um 13:55
> > geschrieben:
> >
> > >
> > > function Part_BOM($PartID, $need, $phase) {
> > >
> > >
> > >
> > >                 global $Invreq;
> >
> >
> > uppercase R !!!
> > And much better is adding it as another parameter and inject it:
> >
> > function Part_BOM($PartID, $need, $phase, $InvReq) { ....
> > }
> >
> > // call it
> > Part_BOM(..., ..., ..., $InvReq);
> >
> > And please read about foreach() and what you can do with it.
> >
> > --
> > PHP General Mailing List (http://www.php.net/) To unsubscribe, visit:
> > http://www.php.net/unsub.php
>
>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
Marco Behnke
Dipl. Informatiker (FH), SAE Audio Engineer Diploma
Zend Certified Engineer PHP 5.3

Tel.: 0174 / 9722336
e-Mail: marco@xxxxxxxxxx

Softwaretechnik Behnke
Heinrich-Heine-Str. 7D
21218 Seevetal

http://www.behnke.biz

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux