PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
alexus
Forum Contributor
Posts: 159 Joined: Fri Jul 04, 2003 10:49 pm
Post
by alexus » Fri Mar 31, 2006 5:45 pm
Hi, I want to assing wariable within function and then call the function so that variables within the function will be avelible to the rest of the script but i can get it working for some reason... can some one advice me on this?
Code: Select all
<?php
test();
echo $ip;
function test(){
$ip = "192.168.0.1";
return $ip;
}
?>
This script returns nothing at all
John Cartwright
Site Admin
Posts: 11470 Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:
Post
by John Cartwright » Fri Mar 31, 2006 5:47 pm
the $ip outside the function and the $ip inside the function are not in the same scope.
Code: Select all
;
echo test();
function test(){
$ip = "192.168.0.1";
return $ip;
}
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Fri Mar 31, 2006 5:47 pm
you forgot to assign the return from the function to a variable.
alexus
Forum Contributor
Posts: 159 Joined: Fri Jul 04, 2003 10:49 pm
Post
by alexus » Fri Mar 31, 2006 5:51 pm
ok, make sance for the cenario i gave...
$ip= test(); is that correct?
What if I have more then one variable that I want to output?
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Fri Mar 31, 2006 5:57 pm
return an array.
alexus
Forum Contributor
Posts: 159 Joined: Fri Jul 04, 2003 10:49 pm
Post
by alexus » Fri Mar 31, 2006 5:57 pm
yes, thats whay I just did
THANKS for help