3

For the next week, I'm stuck with a sadly very slow 1-bar EDGE internet connection, so forgive me if I didn't spend quite enough time researching this one, but I just set up a local server for testing code that I would normally test over the internet, and it doesn't seem to be working the same way on my local LAMP install.

The issue is, when I do this:

echo strtolower($_REQUEST['page']);

the result is this:

files

However, when I do this:

$page['name'] = strtolower($_REQUEST['page']);
echo $page['name'];

the result is this:

f

No, that's not a typo, it consistently returns only the first letter of the string. Doing a var_dump($page) will result in string(5) "files", but doing a var_dump($page['name']) will result in string(1) "f". I'm using PHP 5.2.1.

What is going on here?

5
  • 1
    Are you 100% sure the code you have posted here is identical to what you're actually doing? Commented Aug 31, 2010 at 3:15
  • Where you declared the value of position 'files'? Commented Aug 31, 2010 at 3:16
  • Yes, I literally copied and pasted. Commented Aug 31, 2010 at 3:18
  • Why do a var_dump($page['files'])? Shouldn't you do a var_dump($page['name'])? Commented Aug 31, 2010 at 3:23
  • Yes, that's what I meant. I didn't copy and paste that part, it was actually $page['name']. Commented Aug 31, 2010 at 3:32

3 Answers 3

7

You pretty much answered your own question. $page is "files" (as shown by your first var_dump). This could be caused by the deprecated register_globals, or a manual approximation thereof. Given that,

$page['files']

is "f". This is because non-numeric strings are implicitly converted to 0 (!). You can reproduce this easily with:

$page = 'files';
echo $page['files'];
Sign up to request clarification or add additional context in comments.

3 Comments

I figured it out right before you posted this. Yes, $page was set to files, but I didn't know that. Apparently the issue stems from a PHP setting that was turned on in my local installation but not the remote one which makes it so that if the GET variable "page" is set to files, $page will be set to files as well. Thanks for the help.
@AriX register_globals was on I suppose? Exactly the reason why you always need to initialize your variables properly and turn register_globals off.
Yes, it was register_globals, which I just turned off. I don't know why it was on in the first place - I don't remember turning it on, maybe it came by default with Mac XAMPP? Again, thanks!
2

To sum up: it's a matter of variable declaration. Before using your array, declare it first:

$page = array();

Good luck working with strange server configurations.

Comments

1

Doing a var_dump($page) will result in string(5) "files"

This means the variable $page is a string, not as you seem to expect an array containing a string. You can use array-like offsets on strings as well to return a single character within the string. Through the magic of type casting $string['files'] is equivalent to $string[0], which returns the first character.

Your problem is somewhere where you assign the string to $page, or somewhere after that were you turn it from an array into a single string. The code you provided should work as-is.

I suppose $page is already a string when you assign to $page['name'], so you're actually setting the first character of the string. Try to explicitly declare $page = array().

1 Comment

Yes, that was the issue - I obviously didn't read the output of var_dump clearly enough to realize that it was not an array as I had thought, and I thought that register_globals was off (as it should be).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.