Script Help Please

Twinkie

Banned
Messages
1,389
Reaction score
12
Points
0
Needless to say, I have not scripted in PHP for 3 months now and I have found my self to forget the most basic concepts. Could some one please remind me why this is not working?
PHP:
<?php
$Info = "None";
function LoadResults() {
 $Info = "It is Me!";
 return true;
}

function LogIpCount() {
 $Info = "Foriegn!";
 return false;
}

if ($_SERVER['REMOTE_ADDR']=="000.000.000.000") {LoadResults(); echo "1";}
else {echo "2"; LogIpCount();}

?>

<html>
meaningless code...
<body><?php echo $Info; ?></body>
</html>
I was planning to put a simple IP counter for all IP addresses but my own, like a visit counter. Basically, it checks if I am the person on the web site, otherwise it assigns the value "Foriegn!" to the $Info variable which is printed some ways down the page. Even though I have proven that the IF statement evaluates correctly, for some reason, the function is unable to change the value of the variable and output is the default value: "None". Why is that, especially since it can print the default value?
_____________
Output: "None"

Also, does anybody know why in certain pages, like some of the JavaScript games I have been working on, the IDs and getElementById() does not work? Is it due to improperly nested DIV, TABLES, FIELDSETS, ect?
 
Last edited:

xav0989

Community Public Relation
Community Support
Messages
4,467
Reaction score
95
Points
0
PHP:
<?php
$Info = "None";
function LoadResults() {
 global $Info;
 $Info = "It is Me!";
 return true;
}

function LogIpCount() {
 global $Info;
 $Info = "Foriegn!";
 return false;
}

if ($_SERVER['REMOTE_ADDR'] == "000.000.000.000") {
 LoadResults();
 echo "1";
} else {
 echo "2";
 LogIpCount();
}

?>
<html>
meaningless code...
<body><?php echo $Info; ?></body>
</html>
Since the variable is set outsite, you need to register this variable as a global for the function to work. You may also use pointers... but that's complex.
Twikie said:
Also, does anybody know why in certain pages, like some of the JavaScript games I have been working on, the IDs and getElementById() does not work? Is it due to improperly nested DIV, TABLES, FIELDSETS, ect?
Make sure you are making no mistakes, and look at the javascript error console, it helps.
 

Salvatos

Member
Prime Account
Messages
562
Reaction score
1
Points
18
You could also return $Info instead of true or false I guess.

Also, please note that it is spelled "Foreign" ;)
 

natsuki

New Member
Messages
112
Reaction score
0
Points
0
Even if you change a variables content inside a function, the value of the variable will remain unchanged outside the function unless you declare it as global as xav0989 said. The simplest workaround is of course having the function return the value instead as Salvatos said. The other option is to reference the var but it's not optimal.
 

Twinkie

Banned
Messages
1,389
Reaction score
12
Points
0
Thanks so much guys, It is all coming back to me now! If any of you ever need help with JavaScript/AJAX/HTML, you know were to find me. :biggrin:

Also, the game scripts maybe does contain some errors, but they do not show up in the error console. The script also works perfectly when I use the onclick event (which I hate using), instead of a controlled, script defined event handler. Does anybody know any good JavaScript debuggers that is better than Firefox's error console?

PS: Now I have to read my whole bible sized PHP reference book again. :pat:
 
Last edited:

xav0989

Community Public Relation
Community Support
Messages
4,467
Reaction score
95
Points
0
Does anybody know any good JavaScript debuggers that is better than Firefox's error console?
You might want to take a look at javascript debugger or firebug... extensions.

I've edited the script as you suggested
PHP:
<?php
$Info = "None";
function LoadResults() {
 return "It is Me!";
}

function LogIpCount() {
 return "Foreign!";
}

if ($_SERVER['REMOTE_ADDR'] == "000.000.000.000") {
 $Info = LoadResults();
 echo "1";
} else {
 echo "2";
 $Info = LogIpCount();
}

?>
<html>
meaningless code...
<body><?php echo $Info; ?></body>
</html>
And there was a small spelling mistake too!
 
Last edited:

Twinkie

Banned
Messages
1,389
Reaction score
12
Points
0
Thanks a lot for your help guys! I will look into Firebug. My script is working great now :D
 
Top