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;

 

 

Link to comment
Share on other sites

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
Share on other sites

<?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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.