Jump to content

External Variable in a function


AV1611

Recommended Posts

I have a simple variable like this:

 

$test="sometext";

 

The variable is defined before a function.  when I use the $var in the function, it doesn't "see" it.  If I use the string "sometext" it works fine...  but the string changes, so I need to use a variable... are you allowed to use a $var inside a function that was defined outside a function?

 

Hope that makes sense...

Link to comment
https://forums.phpfreaks.com/topic/50840-external-variable-in-a-function/
Share on other sites

are you looking for something like this?

<?php
function function_name($var) {
$var = 'The value of the variable $test is '.$var; //$var = sometext
return $var;
}

$test="sometext";
echo function_name($test);  //OUTPUT: The value of the variable $test is sometext

?>

It don't work :?????

I get this on screen:

http://xxxxxx.com/webmail/src/redirect.php?login_username=&secretkey=&submit=Login

I know the query has returns because I tested it...

$smfname=$user_info['name'];
$user="xxx";
$host="xxx";
$password="xxx";
$database = "xxxxxx";
mysql_connect($host,$user,$password);
mysql_select_db($database);
$result = mysql_query("SELECT * from truth_webmail where `smfname` like '$smfname'")or die(mysql_error());
if($row=mysql_fetch_array($result)){
	$webmailname=$row['webmailname'];
	$webmailpass=$row['webmailpw'];
	}
function template_menu()
{
global $webmailname, $webmailpass;
//<<<snip>>>
//Added Webmail Button
echo '<td valign="top" class="maintab_back"><a href=http://xxxxxx.com/webmail/src/redirect.php?login_username='.$webmailname.'&secretkey='.$webmailpass.'&submit=Login>WEBMAIL</a></td>';
//<<<snip>>>
}

 

No, that's not needed

 

this is weird... I don't understand what I'm not doing right...

 

here is the actual script:

 

loadUserSettings();
$smfname=$user_info['name'];
//echo $smfname; <----------- This line echo's out ok so I know that info is there
function webmailurl(){
global $smfname;
$user="xxx";
$host="xxx.xxx.xxx.xxx";
$password="xxx";
$database = "xxxxxx";
mysql_connect($host,$user,$password);
mysql_select_db($database);
$result = mysql_query("SELECT * from truth_webmail where `smfname` like '$smfname'")or die(mysql_error());
if($row=mysql_fetch_array($result)){
	$webmailname=$row['webmailname'];
	$webmailpass=$row['webmailpw'];
	}
	echo "<a href='http://xxxxxxxxxxxx.com/webmail/src/redirect.php?login_username=".$webmailname."&secretkey=".$webmailpass."&submit=Login'>WEBMAIL</a>";
}

 

$webmailname & $webmailpass don't echo out...

Last I knew to get a function to use an external variable, you had to include that variable into the function's argument list.

 

like so: function webmailurl($smfname)

 

eating my words here....just really paid attention and noticed the use of global.  my bad...sorry.

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.