Page 1 of 1

function output problem

Posted: Fri Mar 31, 2006 5:45 pm
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:

Posted: Fri Mar 31, 2006 5:47 pm
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;
}

Posted: Fri Mar 31, 2006 5:47 pm
by feyd
you forgot to assign the return from the function to a variable.

Posted: Fri Mar 31, 2006 5:51 pm
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?

Posted: Fri Mar 31, 2006 5:57 pm
by feyd
return an array.

Posted: Fri Mar 31, 2006 5:57 pm
by alexus
yes, thats whay I just did
THANKS for help