Webmaster Forum

Go Back   Webmaster Forum > Web Development > Coding Forum

Coding Forum Problems with your code? Discuss coding issues, including JavaScript, PHP & MySQL, HTML & CSS, Flash & ActionScript, and more.


Reply
 
Thread Tools Display Modes
Share |
  #1  
Old 01-26-2007, 12:11 PM
I like pie's Avatar
I like pie I like pie is offline
Contributing Member
 
Join Date: 10-12-03
Location: California
Posts: 209
iTrader: 0 / 0%
array weirdness in php

I set something up on my dev site the other day, and it works fine. i updated the live site with the current files and everything appeared to be well... except i 'm experiencing something rather strange.

i can set an array to contain a certain value on one site, and the EXACT same code doesn't work on the other site:

Code:
$strip['strip_id'] = $_GET['strip']; foreach($strip as $key => $value) { echo "key is ".$key." and value is ".$value."\n"; } die;
There's no chance it's a typo, they're both pulling the same file from a common repository. but take a look at the pages:

http://dev.foohonpie.net/snat/index.php?strip=20040206

http://foohonpie.net/snat/index.php?strip=20040206

something different is happening here, but i don't know what or why. has anybody had something like this happen to them before?
 
Reply With Quote

Advertisement

Advertisement

  #2  
Old 01-26-2007, 12:54 PM
Taltos's Avatar
Taltos Taltos is offline
v7n Mentor
 
Join Date: 11-22-06
Location: Phoenix, AZ
Posts: 1,797
iTrader: 0 / 0%
Just out of curiosity....if you alter your code to give a simple numeric index to the array and then try to output the contents with a for loop rather than foreach...what happens?

Or just try to output $strip['strip_id'] and see what happens. The only guess I have is that the array has something else in it besides what we saw from your code. That "something else" is somehow an illegal array index or and illegal value. The test above would tell you that the repository code is definitely OK and not the problem. It might, in the case of the for loop, tell you what else is in that array that shouldn't be.

Good luck on that one!
 
Reply With Quote
  #3  
Old 01-26-2007, 01:11 PM
I like pie's Avatar
I like pie I like pie is offline
Contributing Member
 
Join Date: 10-12-03
Location: California
Posts: 209
iTrader: 0 / 0%
well... i output $strip['strip_id'] and get "2"... and i also noticed that it seems to be a problem with the name of the array, and i have no idea why.

this is what i have now:

Code:
$strip['strip_id'] = $_GET['strip']; foreach($strip as $key => $value) { echo "key is ".$key." and value is ".$value."\n"; } echo "<br>$"."strip['strip_id'] is ".$strip['strip_id']; $strip_2['strip_id'] = $_GET['strip']; echo "<br>$"."strip_2['strip_id'] is ".$strip_2['strip_id']; die;
$strip_2 works fine, but $strip is all goofy still. and if i do this:

Code:
$strip = '';
before it, it works. but honestly, i shouldn't have to do that... that's a rather sketchy fix to something that might be indicative of a problem down the road.

again, the same exact file...

http://dev.foohonpie.net/snat/index.php?strip=20040206

http://foohonpie.net/snat/index.php?strip=20040206

I'll try that for loop thing.. it'd be nice to see exactly what it is that i'm clearing out when setting $strip to null.

Last edited by I like pie; 01-26-2007 at 01:17 PM.
 
Reply With Quote
  #4  
Old 01-26-2007, 01:26 PM
I like pie's Avatar
I like pie I like pie is offline
Contributing Member
 
Join Date: 10-12-03
Location: California
Posts: 209
iTrader: 0 / 0%
alright... this is making some sense now

i think what's going on is that the $strip is being preset as $_GET['strip']... so when i try to set $strip['anything'] to anything, it screws up because it already exists as a non-array variable.

that's my guess anyway. would explain why $strip_2['foo'] works fine.

but what it doesn't explain though, is why it works on one site but not on the other. they should have the exact same php configurations.
 
Reply With Quote
  #5  
Old 01-26-2007, 01:33 PM
I like pie's Avatar
I like pie I like pie is offline
Contributing Member
 
Join Date: 10-12-03
Location: California
Posts: 209
iTrader: 0 / 0%
and i've found the problem...

dev.foohonpie.net: php 5.1.2
foohonpie.net: mod_php 4.4.2

i really should have checked that in the beginning haha. probably not good that my dev site is more up to date than the live one... i hope nothing breaks
 
Reply With Quote
  #6  
Old 01-26-2007, 03:34 PM
Taltos's Avatar
Taltos Taltos is offline
v7n Mentor
 
Join Date: 11-22-06
Location: Phoenix, AZ
Posts: 1,797
iTrader: 0 / 0%
I have to say I blew off different hosts or configurations. I saw the url and assumed one was a subdomain with all the same toys. Important part is you got it figured out! Bonus points for that
 
Reply With Quote
  #7  
Old 01-26-2007, 06:36 PM
Hades's Avatar
Hades Hades is offline
Senior Web Developer
 
Join Date: 01-23-07
Location: Argentina
Posts: 2,273
iTrader: 0 / 0%
Hi,

It is not about the version of PHP, but it should happen because one server has the REGISTER GLOBALS in ON, and in the other server it could be in OFF.

If you use global variables like $_GET, $_SESSION, and have the REGISTER_GLOBALS = ON, you should be careful with the names of the variables,

IE, if you set up a variable like this:
$_SESSION['name'] = "Jack Ryan";
or $_GET['name'] = "Jack Ryan";

then use a local variable:
$name = "Cathy Ryan";

the second variable will override the first one. That will not happen in a server with REGISTER_GLOBALS = OFF.

In your script you wrote:

PHP Code:
$strip['strip_id'] = $_GET['strip'];

foreach(
$strip as $key => $value) {
  echo 
"key is ".$key." and value is ".$value."\n";
}
die; 
You are overwriting the $_GET['strip'] variable with a new one called $strip and setting it up as an array which contains "strip_id", it makes the script messing all up.

Good Luck!
 
Reply With Quote
  #8  
Old 01-27-2007, 12:36 AM
I like pie's Avatar
I like pie I like pie is offline
Contributing Member
 
Join Date: 10-12-03
Location: California
Posts: 209
iTrader: 0 / 0%
I do recall reading about register globals years ago. Never thought anything of it until now.

Thanks guys.
 
Reply With Quote
Go Back   Webmaster Forum > Web Development > Coding Forum

Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
imploding array keys I like pie Coding Forum 7 12-28-2006 05:55 PM
more weirdness... with adsense chillfire Contextual Networks 5 12-14-2006 05:37 AM
quick array question I like pie Coding Forum 1 12-02-2006 10:05 PM
Adding 1 to array? spl1nter Coding Forum 4 09-14-2004 07:22 PM


V7N Network
Get exposure! V7N I Love Photography V7N SEO Blog V7N Directory


All times are GMT -7. The time now is 10:40 PM.
Powered by vBulletin
Copyright 2000-2014 Jelsoft Enterprises Limited.
Copyright © 2003 - 2018 VIX-WomensForum LLC