Jump to content

[SOLVED] passing a variable from a function


wrathican

Recommended Posts

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;

 

 

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;

<?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;

?>

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;

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.