View Full Version : Arrays in and outside of Functions
alex weber
06-19-2010, 11:49 AM
have an array defined outside of a function. Both are inside of a file I include other places, and I use filterFunc(); on those other pages. In order for the function to run through the array, I need to define the array inside of it, as well. Unfortunately, I need to use the array elsewhere and don't feel like going through the globalizing thing. Here's what I have:
PHP Code:
$filterArr = array(
"all" => "All",
"sn" => "S/N",
"tags" => "Tags",
"vendor" => "Vendor",
"purdate" => "Pur Date",
"ticketnum" => "Ticket Num");
function filterFunc() {
$filterArr = array(
"all" => "All",
"sn" => "S/N",
"tags" => "Tags",
"vendor" => "Vendor",
"purdate" => "Pur Date",
"ticketnum" => "Ticket Num");
foreach($filterArr as $key => $val){
echo '<option value="'.$key.'">'.$val.'</option>'."\n";
}
}
Is there a reason why I can't just use $filterArr without redefining it inside of filterFunc();?
djr33
06-19-2010, 12:12 PM
Add this line to the top of your function:
global $filterArr;
If you really "don't feel like globalizing it", then you will need to do what you're doing.
alex weber
07-18-2010, 01:13 PM
!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!
I'm not sure what you mean by that, but if you want to know why you "can't just use it," then I'll tell you: PHP is designed like that on purpose. Variables (arrays, functions, etc.) in one scope (say, outside a function) are invisible to those in another (say, inside that function). As frustrating as that may be at times, it's in place for a good reason: it prevents accidental conflicts (like accidentally overwriting an important variable when executing an outside function that happens to have a variable with the same name). If you want to know more, read here (http://php.net/manual/en/language.variables.scope.php).
It's too bad you "don't feel like going through the globalizing thing," because that's basically the only way to get around redefining the variable inside your function (or passing it in through the function call).
But it's not that hard.
As djr said,
<?php
$filterArr = array(
"all" => "All",
"sn" => "S/N",
"tags" => "Tags",
"vendor" => "Vendor",
"purdate" => "Pur Date",
"ticketnum" => "Ticket Num"
);
function filterFunc() {
// this is basically Example #1 from the php.net link I gave above
global $filterArr;
foreach($filterArr as $key => $val){
echo '<option value="'.$key.'">'.$val.'</option>'."\n";
}
}
?>
also, you seem to have a bunch of random hyperlinks in your last post. What gives?
djr33
07-19-2010, 01:30 AM
1. I removed a spam reply and banned the user who is now also the original poster, so I will additionally close this discussion.
2. One note to add to this slightly outdated discussion, for anyone else who might have a similar question is:
$var = $GLOBALS['var'];
That is another way to access the value of a global variable from within a function. Alternatively you can use/modify $GLOBALS['var'] directly.
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.