function output problem

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

Post Reply
alexus
Forum Contributor
Posts: 159
Joined: Fri Jul 04, 2003 10:49 pm

function output problem

Post by alexus »

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 :roll:
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

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;
}
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

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 »

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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

return an array.
alexus
Forum Contributor
Posts: 159
Joined: Fri Jul 04, 2003 10:49 pm

Post by alexus »

yes, thats whay I just did
THANKS for help
Post Reply