RE: [PHP] global array
Jeff Burcher j...@allredmetal.com 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 j...@allredmetal.com -Original Message- From: ma...@behnke.biz [mailto:ma...@behnke.biz] Sent: Thursday, June 14, 2012 8:04 AM To: php-general@lists.php.net; j...@allredmetal.com Subject: Re: [PHP] global array Jeff Burcher j...@allredmetal.com 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: ma...@behnke.biz 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
[PHP] global array
Hi, I am running PHP 5.4 on IIs 6 on a Windows SBS 2003 server. Here is a streamlined version of the code I am dealing with. I tried to trim as much as possible to only show code that deals with my issue. The main issue I think I am having is the global array statement within the function is not working. From all of the articles I have read, I seem to be doing it correctly, yet it does not recognize $InvReq as an array within the function. The array works just fine outside of the function, though. Here are the error messages: Notice: Undefined variable: InvReq (line numbers point to inside the function) Warning: array_key_exists() expects parameter 2 to be array, null given (line numbers point to inside the function) If anyone could point out where I have incorrect syntax or something else that would interfere with the global reference for the array within the function, or tell me I can't do what I am doing using arrays, but assume I don't have the ability to write to an external work table, which is why I am trying to use an array in the first place. Thanks. - ?php $InvReq = array(); // there is some database action here and other programming, but the core issue is writing to the array, so assume I have all of the necessary data // data loop to gather inventory requests --- $linetotal = ???; $hldpartID = ?; //these variables are provided by looping, so this check/write/update code snippet happens many times to update/write to the array if (array_key_exists($hldpartID, $InvReq)) { $InvReq[$hldpartID] += $linetotal; //if this line will create the array entry if it does not exist, then I don't need the key_exists check, anyone?? } else { $InvReq[$hldpartID] = $linetotal; } Part_BOM($hldpartID, $linetotal, 1); //function is called the first time to set up the BOM // end loop --- // after gathering all of the inventory requests, check for inventory on hand reset ($InvReq); while (list($PartID,) = each($InvReq)) { // data loop to get inventory on hand --- $OnHandQty = ???; //these variables are provided by looping, so this check/write/update code snippet happens many times to update/add to the array if (array_key_exists($PartID, $InvReq)) { $InvReq[$PartID] -= $OnHandQty; } else { $InvReq[$PartID] = $OnHandQty; } Part_BOM($PartID, $OnHandQty, 2); //function is called the second time to trim up BOM // end loop --- } // print list $display_block = h2Part Forecast/h2tabletrthPart Number/ththAmount Needed/th/tr; reset ($InvReq); while (list($PartID,$need) = each($InvReq)) { $needout = number_format($need); $display_block .= trtd$PartID/tdtd$needout/td/tr; } $display_block .= /table; //---this function adds or subtracts inventory to/from the BOM materials for the part passed. //---it also can add parts that make parts with their inventory total for a multiple level BOM function Part_BOM($PartID, $need, $phase) { global $Invreq; $BOMreq = $need * $BOMQty; //$BOMQty $BOMPartID are pulled from a database keyed by $PartID if ($phase == 1) { if (array_key_exists($BOMPartID, $InvReq)) { $InvReq[$BOMPartID] += $BOMreq; //first time through adds to array item totals } else { $InvReq[$BOMPartID] = $BOMreq; } } else { if (array_key_exists($BOMPartID, $InvReq)) { $InvReq[$BOMPartID] -= $BOMreq; //second time through subtracts from array item totals } } if (--check for parts within other parts for multiple level BOM--) { Part_BOM($BOMPartID, $BOMreq, $phase); //this calls itself and can refer/loop back to itself several times } } ? html head /head body center ?php echo $display_block ? /center /body /html -- Thanks for your input, Jeff Burcher - IT Dept Allred Metal Stamping PO Box 2566 High Point, NC 27261 (336)886-5221 x229
Re: [PHP] global array
Jeff Burcher j...@allredmetal.com 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
RE: [PHP] global array
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? Thanks, Jeff Burcher - IT Dept Allred Metal Stamping PO Box 2566 High Point, NC 27261 (336)886-5221 x229 j...@allredmetal.com -Original Message- From: ma...@behnke.biz [mailto:ma...@behnke.biz] Sent: Thursday, June 14, 2012 8:04 AM To: php-general@lists.php.net; j...@allredmetal.com Subject: Re: [PHP] global array Jeff Burcher j...@allredmetal.com 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
Re: [PHP] global array
Yes - PHP is very picky. Hence I never capitalize anything! I use underscores to make varnames more understandable, as in $inv_req -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] global array
On 6/14/2012 12:49 PM, Jim Giner wrote: Yes - PHP is very picky. Hence I never capitalize anything! I use underscores to make varnames more understandable, as in $inv_req There is another nice custom e.g. $invReg it's easy to read and it doesn't conflict with PHP syntax for some functions e.g., in_aray(). and defines DOCUMENT_ROOT -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] global array
Al n...@ridersite.org wrote in message news:6b.c0.39100.4ef1a...@pb1.pair.com... On 6/14/2012 12:49 PM, Jim Giner wrote: Yes - PHP is very picky. Hence I never capitalize anything! I use underscores to make varnames more understandable, as in $inv_req There is another nice custom e.g. $invReg it's easy to read and it doesn't conflict with PHP syntax for some functions e.g., in_aray(). and defines DOCUMENT_ROOT And what is that custom? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] global array
On Thu, 2012-06-14 at 15:13 -0400, Jim Giner wrote: Al n...@ridersite.org wrote in message news:6b.c0.39100.4ef1a...@pb1.pair.com... On 6/14/2012 12:49 PM, Jim Giner wrote: Yes - PHP is very picky. Hence I never capitalize anything! I use underscores to make varnames more understandable, as in $inv_req There is another nice custom e.g. $invReg it's easy to read and it doesn't conflict with PHP syntax for some functions e.g., in_aray(). and defines DOCUMENT_ROOT And what is that custom? I think he means camelCase naming syntax. It does tend to involve fewer keystrokes and results in shorter names that might more easily fit to a screen or paper, but I have to say I prefer the underscore convention, it just makes things much more readable (which is probably why built-in PHP functions and constants use it) -- Thanks, Ash http://www.ashleysheridan.co.uk
Re: [PHP] global array
See - I didn't even notice he used camel-case - I thought he typed the same thing that got the OP in trouble. See how difficult that custom is? That's why for any case sensitive syntax, I stick to all lower case to avoid just that kind of bug-a-boo. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] global array, can't assign values from variables
Hi I'm just going to give the code and output. It should be self explanatory. The array, $criteria, is having the issue. I don't know what it's doing. I cannot seem to assign the value from the $data variable to the $criteria[index] value. You will see some attempts to debug the situation which leads me to more puzzlement. CODE: // run when cdata is found function characterDataHandler($parser, $data) { switch( $GLOBALS['currentTag'] ) { case MINSTARTDATE : echo $data;/*$GLOBALS['criteria']['minstartdate'] = $data;*/break; case MAXSTARTDATE : $GLOBALS['criteria']['maxstartdate'] = junk;/*$data;*/break; case MINSTAY : $GLOBALS['criteria']['minstay'] = $data; break; case MAXSTAY : $GLOBALS['criteria']['maxstay'] = $data; break; case MINRENT : $GLOBALS['criteria']['minrent'] = $data; break; case MAXRENT : $GLOBALS['criteria']['maxrent'] = $data; break; case RENTINC : $GLOBALS['criteria']['rentinc'] = $data; break; case MINBEDS : $GLOBALS['criteria']['minbeds'] = $data; break; case MAXBEDS : $GLOBALS['criteria']['maxbeds'] = $data; break; case PROPCOUNT : $GLOBALS['criteria']['propcount'] = $data; break; default: break; } } echo pre\n; print_r($criteria); echo /pre\n; OUTPUT: 9/6/2003 Array ( [maxstartdate] = junk [minstay] = [maxstay] = [minrent] = [maxrent] = [rentinc] = [minbeds] = [maxbeds] = [propcount] = ) Thanks.-- Chris Edwards Web Application Developer Outer Banks Internet, Inc. 252-441-6698 [EMAIL PROTECTED] http://www.OuterBanksInternet.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] global array, can't assign values from variables
Chris Edwards wrote: I'm just going to give the code and output. It should be self explanatory. The array, $criteria, is having the issue. I don't know what it's doing. I cannot seem to assign the value from the $data variable to the $criteria[index] value. You will see some attempts to debug the situation which leads me to more puzzlement. CODE: // run when cdata is found function characterDataHandler($parser, $data) { switch( $GLOBALS['currentTag'] ) { case MINSTARTDATE : echo $data;/*$GLOBALS['criteria']['minstartdate'] = $data;*/break; case MAXSTARTDATE : $GLOBALS['criteria']['maxstartdate'] = junk;/*$data;*/break; case MINSTAY : $GLOBALS['criteria']['minstay'] = $data; break; [snip] echo pre\n; print_r($criteria); echo /pre\n; OUTPUT: 9/6/2003 Array ( [maxstartdate] = junk [minstay] = [snip] $data is empty. How are you calling this function? -- ---John Holmes... Amazon Wishlist: www.amazon.com/o/registry/3BEXC84AB3A5E/ php|architect: The Magazine for PHP Professionals www.phparch.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] global array, can't assign values from variables SOLVED
Hi $data is not empty. It's obvious from the : OUTPUT: 9/6/2003 Anyways, it is overwriting itself because of begin and start tags. I didn't realize that. Thanks. -- Chris Edwards Web Application Developer Outer Banks Internet, Inc. 252-441-6698 [EMAIL PROTECTED] http://www.OuterBanksInternet.com - Original Message - From: John W. Holmes [EMAIL PROTECTED] To: Chris Edwards [EMAIL PROTECTED] Cc: [EMAIL PROTECTED] Sent: Friday, September 05, 2003 4:11 PM Subject: Re: [PHP] global array, can't assign values from variables Chris Edwards wrote: I'm just going to give the code and output. It should be self explanatory. The array, $criteria, is having the issue. I don't know what it's doing. I cannot seem to assign the value from the $data variable to the $criteria[index] value. You will see some attempts to debug the situation which leads me to more puzzlement. CODE: // run when cdata is found function characterDataHandler($parser, $data) { switch( $GLOBALS['currentTag'] ) { case MINSTARTDATE : echo $data;/*$GLOBALS['criteria']['minstartdate'] = $data;*/break; case MAXSTARTDATE : $GLOBALS['criteria']['maxstartdate'] = junk;/*$data;*/break; case MINSTAY : $GLOBALS['criteria']['minstay'] = $data; break; [snip] echo pre\n; print_r($criteria); echo /pre\n; OUTPUT: 9/6/2003 Array ( [maxstartdate] = junk [minstay] = [snip] $data is empty. How are you calling this function? -- ---John Holmes... Amazon Wishlist: www.amazon.com/o/registry/3BEXC84AB3A5E/ php|architect: The Magazine for PHP Professionals www.phparch.com -- 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
Re: [PHP] global array, can't assign values from variables
* Thus wrote Chris Edwards ([EMAIL PROTECTED]): Hi I'm just going to give the code and output. It should be self explanatory. The array, $criteria, is having the issue. I don't know what it's doing. I cannot seem to assign the value from the $data variable to the $criteria[index] value. You will see some attempts to debug the situation which leads me to more puzzlement. This code can't be clearly evaluated, There are several things that can be wrong CODE: switch( $GLOBALS['currentTag'] ) { case MINSTARTDATE : echo $data;/*$GLOBALS['criteria']['minstartdate'] = $data;*/break; case MAXSTARTDATE : $GLOBALS['criteria']['maxstartdate'] = junk;/*$data;*/break; case MINSTAY : $GLOBALS['criteria']['minstay'] = $data; break; case MAXSTAY : $GLOBALS['criteria']['maxstay'] = $data; break; case MINRENT : $GLOBALS['criteria']['minrent'] = $data; break; case MAXRENT : $GLOBALS['criteria']['maxrent'] = $data; break; case RENTINC : $GLOBALS['criteria']['rentinc'] = $data; break; case MINBEDS : $GLOBALS['criteria']['minbeds'] = $data; break; case MAXBEDS : $GLOBALS['criteria']['maxbeds'] = $data; break; case PROPCOUNT : $GLOBALS['criteria']['propcount'] = $data; break; default: break; } global $currentTag may be: not assigned not all uppercase have a leading or trailing space not be one of the 'case' values $data is empty. Curt -- I used to think I was indecisive, but now I'm not so sure. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] help with PHP global array
Hi all, How can I declare a global array? Can I do this: $global myArray[] would I find out it is an array when I use it in other places? Thanks, Wee -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: help with PHP global array
$array = Array('a'=1); function scope_test() { global $array; echo $array['a']; } Works for me, PHP 4.1.1 with the new .ini file. -- Julio Nobrega. Um dia eu chego lá: http://sourceforge.net/projects/toca Ajudei? Salvei? Que tal um presentinho? http://www.submarino.com.br/wishlistclient.asp?wlid=664176742884 Wee Chua [EMAIL PROTECTED] wrote in message [EMAIL PROTECTED]">news:[EMAIL PROTECTED]... Hi all, How can I declare a global array? Can I do this: $global myArray[] would I find out it is an array when I use it in other places? Thanks, Wee -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] help with PHP global array
Hi all, I have tried: global $myArray[] and it gives me an error 'the array expects semi-colon or comma'. Any helps would be appreciated! Thanks, Wee -Original Message- From: Bjorn Abt [mailto:[EMAIL PROTECTED]] Sent: Thursday, February 07, 2002 10:57 AM To: '[EMAIL PROTECTED]' Subject: AW: [PHP] help with PHP global array I would try: global $myArray[] Greetings Björn -Ursprüngliche Nachricht- Von: Wee Chua [ mailto:[EMAIL PROTECTED] mailto:[EMAIL PROTECTED] ] Gesendet: Donnerstag, 7. Februar 2002 16:35 An: PHP (E-mail) Betreff: [PHP] help with PHP global array Hi all, How can I declare a global array? Can I do this: $global myArray[] would I find out it is an array when I use it in other places? Thanks, Wee -- PHP General Mailing List ( http://www.php.net/ http://www.php.net/ ) To unsubscribe, visit: http://www.php.net/unsub.php http://www.php.net/unsub.php
Re: [PHP] help with PHP global array
On Thursday 07 February 2002 23:48, Wee Chua wrote: Hi all, I have tried: global $myArray[] and it gives me an error 'the array expects semi-colon or comma'. Any helps would be appreciated! Thanks, Wee -Original Message- From: Bjorn Abt [mailto:[EMAIL PROTECTED]] Sent: Thursday, February 07, 2002 10:57 AM To: '[EMAIL PROTECTED]' Subject: AW: [PHP] help with PHP global array I would try: global $myArray[] Greetings Björn -Ursprüngliche Nachricht- Von: Wee Chua [ mailto:[EMAIL PROTECTED] mailto:[EMAIL PROTECTED] ] Gesendet: Donnerstag, 7. Februar 2002 16:35 An: PHP (E-mail) Betreff: [PHP] help with PHP global array Hi all, How can I declare a global array? Can I do this: $global myArray[] would I find out it is an array when I use it in other places? Thanks, Wee All variables in PHP are local to the scope in which it is defined. You cannot declare a variable as global (as such). To use a global variable: ? $doodah = 10; test_global(); function test_global() { global $doodah; echo(Doodah should be ten: $doodah); } ? See manual for details. -- Jason Wong - Gremlins Associates - www.gremlins.com.hk /* A lot of people I know believe in positive thinking, and so do I. I believe everything positively stinks. -- Lew Col */ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] help with PHP global array
All variables in PHP are local to the scope in which it is defined. You cannot declare a variable as global (as such). To use a global variable: This is true. But I believe that the original question was actually the other way around... Make a local variable into a global one. The way to do that is this: ? myFunc() { $myFuncLocalVar = Hello!; echo $myFuncLocalVar; $GLOBALS[myFuncLocalVar] = $myFuncLocalVar; } myFunc(); echo $myFuncLocalVar; ? Chris -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php