AV1611 Posted May 10, 2007 Share Posted May 10, 2007 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... Quote Link to comment https://forums.phpfreaks.com/topic/50840-external-variable-in-a-function/ Share on other sites More sharing options...
genericnumber1 Posted May 10, 2007 Share Posted May 10, 2007 http://us2.php.net/manual/en/language.variables.scope.php Quote Link to comment https://forums.phpfreaks.com/topic/50840-external-variable-in-a-function/#findComment-250032 Share on other sites More sharing options...
bbaker Posted May 10, 2007 Share Posted May 10, 2007 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 ?> Quote Link to comment https://forums.phpfreaks.com/topic/50840-external-variable-in-a-function/#findComment-250060 Share on other sites More sharing options...
AV1611 Posted May 10, 2007 Author Share Posted May 10, 2007 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>>> } Quote Link to comment https://forums.phpfreaks.com/topic/50840-external-variable-in-a-function/#findComment-250061 Share on other sites More sharing options...
AV1611 Posted May 10, 2007 Author Share Posted May 10, 2007 even easier and it still don't work: $smfname=$user_info['name']; function webmailurl() { global $smfname; echo $smfname; } webmailurl(); no return... Quote Link to comment https://forums.phpfreaks.com/topic/50840-external-variable-in-a-function/#findComment-250076 Share on other sites More sharing options...
bbaker Posted May 10, 2007 Share Posted May 10, 2007 you're missing the variable within the () $smfname=$user_info['name']; function webmailurl($smfname) { global $smfname; echo $smfname; } webmailurl($smfname); Quote Link to comment https://forums.phpfreaks.com/topic/50840-external-variable-in-a-function/#findComment-250077 Share on other sites More sharing options...
AV1611 Posted May 10, 2007 Author Share Posted May 10, 2007 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... Quote Link to comment https://forums.phpfreaks.com/topic/50840-external-variable-in-a-function/#findComment-250092 Share on other sites More sharing options...
bbaker Posted May 10, 2007 Share Posted May 10, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/50840-external-variable-in-a-function/#findComment-250103 Share on other sites More sharing options...
AV1611 Posted May 10, 2007 Author Share Posted May 10, 2007 I don't know why it doesn't work on this script but I did it by passing the $var as $_SESSION[var]... worked but I still am confused by the other... Quote Link to comment https://forums.phpfreaks.com/topic/50840-external-variable-in-a-function/#findComment-250186 Share on other sites More sharing options...
Barand Posted May 10, 2007 Share Posted May 10, 2007 At what point in the code is the function called? Quote Link to comment https://forums.phpfreaks.com/topic/50840-external-variable-in-a-function/#findComment-250197 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.