wrathican Posted January 24, 2008 Share Posted January 24, 2008 hi there what im trying to do is use some variables to create another variable inside a function. then what i need is that final variable to be accessible outside of the function. but i cant seem to get it to work. here is the function.: function full_url() { $s = empty($_SERVER["HTTPS"]) ? '' : ($_SERVER["HTTPS"] == "on") ? "s" : ""; $protocol = substr(strtolower($_SERVER["SERVER_PROTOCOL"]), 0, strpos(strtolower($_SERVER["SERVER_PROTOCOL"]), "/")) . $s; $addr = $protocol . "://" . $_SERVER['SERVER_NAME']; } full_url(); //the $addr is the var i want to be able to access //i want to be able to use the variable here in say an echo statement like so: echo $addr; Link to comment https://forums.phpfreaks.com/topic/87530-solved-passing-a-variable-from-a-function/ Share on other sites More sharing options...
rajivgonsalves Posted January 24, 2008 Share Posted January 24, 2008 it should be function full_url() { $s = empty($_SERVER["HTTPS"]) ? '' : ($_SERVER["HTTPS"] == "on") ? "s" : ""; $protocol = substr(strtolower($_SERVER["SERVER_PROTOCOL"]), 0, strpos(strtolower($_SERVER["SERVER_PROTOCOL"]), "/")) . $s; $addr = $protocol . "://" . $_SERVER['SERVER_NAME']; return $addr; } $addr = full_url(); //the $addr is the var i want to be able to access //i want to be able to use the variable here in say an echo statement like so: echo $addr; Link to comment https://forums.phpfreaks.com/topic/87530-solved-passing-a-variable-from-a-function/#findComment-447698 Share on other sites More sharing options...
trq Posted January 24, 2008 Share Posted January 24, 2008 <?php function full_url() { $s = empty($_SERVER["HTTPS"]) ? '' : ($_SERVER["HTTPS"] == "on") ? "s" : ""; $protocol = substr(strtolower($_SERVER["SERVER_PROTOCOL"]), 0, strpos(strtolower($_SERVER["SERVER_PROTOCOL"]), "/")) . $s; return $protocol . "://" . $_SERVER['SERVER_NAME']; } $addr = full_url(); //the $addr is the var i want to be able to access //i want to be able to use the variable here in say an echo statement like so: echo $addr; ?> Link to comment https://forums.phpfreaks.com/topic/87530-solved-passing-a-variable-from-a-function/#findComment-447700 Share on other sites More sharing options...
rajesh Posted January 24, 2008 Share Posted January 24, 2008 Try this code global $addr ; function full_url() { global $addr ; $s = empty($_SERVER["HTTPS"]) ? '' : ($_SERVER["HTTPS"] == "on") ? "s" : ""; $protocol = substr(strtolower($_SERVER["SERVER_PROTOCOL"]), 0, strpos(strtolower($_SERVER["SERVER_PROTOCOL"]), "/")) . $s; $addr = $protocol . "://" . $_SERVER['SERVER_NAME']; } full_url(); //the $addr is the var i want to be able to access //i want to be able to use the variable here in say an echo statement like so: echo $addr; Link to comment https://forums.phpfreaks.com/topic/87530-solved-passing-a-variable-from-a-function/#findComment-447702 Share on other sites More sharing options...
wrathican Posted January 24, 2008 Author Share Posted January 24, 2008 oh thank god. i never knew you could do that with a function. i guess because the function only returns a string you can asign that tring to a var. excellent thanks alot! Link to comment https://forums.phpfreaks.com/topic/87530-solved-passing-a-variable-from-a-function/#findComment-447704 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.