views:

83

answers:

7

I'm having trouble with the following code. What it should do is echo cats.php followed by example.php but it's not echoing the example.php. Any ideas why this might be happening?

$bookLocations = array(
    'example.php',
    'cats.php',
    'dogs.php',
    'fires.php',
    'monkeys.php',
    'birds.php',
);

echo $bookLocations[1];

function findfile($filenumber)
{
echo $bookLocations["$filenumber"];
}

findfile(0);
+6  A: 

Try changing,

echo $bookLocations["$filenumber"];

to:

echo $bookLocations[$filenumber];

Edit* To expand on Thomas's correct answer, instead of using global variables, you could change your method to:

function findfile($filenumber, $bookLocations)
{
    echo $bookLocations[$filenumber];
}
Magic Hat
Indeed. "thomas" has the right answer below. You need to use global $foo; within your function. It's a scoping issue.
genio
Better try to `echo $bookLocations;`
Ivan Nevostruev
not the most important reason why it doesn't work!
tharkun
@genio: that's bad advice, global variables are evil!
tharkun
Heh dunno how I overlooked that. Should probably just change to pass the array as a parameter and then use it that way.
Magic Hat
+1 because this answer is clearly better than the current accepted answer http://stackoverflow.com/questions/1555521/my-php-function-isnt-working/1555541#1555541
MiseryIndex
+5  A: 

i believe you may also need to declare the global variable in your function.

global $bookLocations;
thomas
-1 global variables are bad!
tharkun
don't disagree with you but given the function provided, that is the only way. Some of the suggestions to change the function declaration are also quite valid.
thomas
Thats another issue, tharkun. He could have mentioned that in his answer, but this is obviously what the GP was trying to do, so this is the correct answer.
gnud
no, it's not the correct answer! it's the wrong answer! the correct answer is to pass the array to the function!
tharkun
you should really not accept this answer because you will help promoting bad practices! using globals is a bad practice in general!
tharkun
woo guys, who is upvoting this? it is bad advise. period.
tharkun
LOL, tharkun, calm down. :) Let him learn it the hard way. :P
MiseryIndex
I'm privatly calm :) but SO-ly agitated when obviously wrong answers are being accepted and upvoted ;)
tharkun
Well, it'll solve his problem. At least for a little while. And hopefully encourage him to find out why global variables are bad.
MiseryIndex
wuhhuhubrrr-shudder. ok :)
tharkun
A: 

You dont need the quote marks in the function.

Visage
+1  A: 

The quotes in "$filenumber" turn your key into a string, when the keys to your array are all numbers. You are trying to access $bookLocations["1"] when in fact you want to access $bookLocations[1] -- that is to say, 1 is not the same as "1". Therefore, like others have said, you need to get rid of the quotation marks around the key (and check your variable scope too).

Mark Rushakoff
A: 

$bookLocations is out of scope for your function. If you echo $filenumber you will see that it's in scope because you passed it in by value. However, there is no reference to $bookoLocations.

You should pass in $bookLocations

declaration: function findfile($filenumber, $bookLocations){ call: findfile(1, $bookLocations);

You could also to declare $bookLocations as global, but globals should be avoided if possible.

easement
+3  A: 

Ok, there are two issues.

Variable Scope

Your function doesn't know the array $bookLocations, you need to pass it to your function like so:

function findfile($filenumber, $bookLocations)

Array key

You don't want to wrap your array key in quotes:

wrong: $bookLocations["$filenumber"];
right: $bookLocations[$filenumber];
tharkun
Why `wrong: $bookLocations["$filenumber"];` is wrong?
Ivan Nevostruev
Because the array key is $filenumber not $filenumber wrapped in quotes. It is not a string but it is an integer.
tharkun
This depends on PHP configuration. It is preferable to avoid parentheses, though.
Andrejs Cainikovs
+1 to aid tharkun in his crusade against global variables!
MiseryIndex
+1  A: 
function findfile($filenumber)
{
  global $bookLocations;
  echo $bookLocations[$filenumber];
}

Good-style developers usually avoid global variables. Instead, pass the array to the function as the parameter:

function findfile($files, $filenum)
{
  echo $files[$filenum];
}
Andrejs Cainikovs