avo Posted February 23, 2007 Share Posted February 23, 2007 Hi All im sure there is an easy answer to this one . i have created a function in one file i am calling this function from another file but when this function is called i dont see the return of the variable from the function my code is //included file function test(){ $error = "testing var from included function file"; return $error ; } //outputed file test() ; <?=$error ;?> all helps appriciated. Link to comment https://forums.phpfreaks.com/topic/39781-variable-from-function/ Share on other sites More sharing options...
monk.e.boy Posted February 23, 2007 Share Posted February 23, 2007 //outputed file $error = test() ; <?=$error ;?> or <?php echo test() ; ?> The echo will echo the results of test(), which is a string, so everything works. monk.e.boy Link to comment https://forums.phpfreaks.com/topic/39781-variable-from-function/#findComment-192080 Share on other sites More sharing options...
obsidian Posted February 23, 2007 Share Posted February 23, 2007 Right, in a little further explanation of what monk.e.boy has shown: when a function returns a value, you have to do something with that value. Whether you are assigning the returned value to a variable or printing it out to the screen, you must be proactive to catch the returned value. Many types of functions return a boolean TRUE or FALSE, and that's why they can be used in if statements and such, too: if statements use boolean values for their conditions. Hope that helps. Link to comment https://forums.phpfreaks.com/topic/39781-variable-from-function/#findComment-192086 Share on other sites More sharing options...
avo Posted February 23, 2007 Author Share Posted February 23, 2007 Hi Thankyou both i had a brain freeze moment. this is the code i was using for the question but i had to change the case from (0) to 1 and 2 else the returned value was o and not the variable why would this be ? //function.php file function login($case){ require_once ('./includes/db/mydatabasefile'); //connect to db $con = mysql_connect($db_host,$db_user,$db_pass); $sel = mysql_select_db($db_name); switch ($case) { case 1: $sql = mysql_query( "SELECT username,password FROM surv_users WHERE username='".$_POST['txt_username']."' AND password='".$_POST['txt_password']."'" ) or die (mysql_error()); break; case 2: $sql = mysql_query( "SELECT email FROM surv_users WHERE email='".$_POST['txt_email']."'" ) or die (mysql_error()); break; } if( $row = mysql_num_rows($sql)==1){ $_SESSION['user']=true ; $error = false ; }elseif( $row = mysql_num_rows($sql)==0){ $error = true ; //more than one user with same name ect.. to email admin }elseif( $row = mysql_num_rows($sql)>1){ $error = true ; //send error email // Recipient $to = "$admin_email"; $from = "$admin_email_from <$admin_email_from>\r\n"; // Subject $subject = "Error with $url_name"; // Message $message = "This message as been sent."; // Headers $headers = 'MIME-Version: 1.0' . "\r\n"; $headers = 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; $headers .= "From: ".$from."" . "\r\n"; // Mail it mail($to, $subject, $message, $headers); } return ($error) ; } //index.php file //login check if ($_POST['btn_enter']){ $error = ( login(1) ); if ($error==true){ $error = "Sorry we are unable to find <br/>"." your records.<br/>"."Please check your username & password.".""; } } //forgten details check if ($_POST['btn_forgot']){ $error = ( login(2) ); if ($error==true){ $error = "Sorry we are unable to find <br/>"." your email address.<br/>"; } } Link to comment https://forums.phpfreaks.com/topic/39781-variable-from-function/#findComment-192091 Share on other sites More sharing options...
monk.e.boy Posted February 23, 2007 Share Posted February 23, 2007 login(1); means you pass 1 to the function login. In login this is now in the variable $case. Am I misunderstanding your question? monk.e.boy Link to comment https://forums.phpfreaks.com/topic/39781-variable-from-function/#findComment-192132 Share on other sites More sharing options...
avo Posted February 23, 2007 Author Share Posted February 23, 2007 Hi Thankyou again yes but thats ok all help and input is appriciated i did find out what i was doing to create the error but all is functional now. Link to comment https://forums.phpfreaks.com/topic/39781-variable-from-function/#findComment-192135 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.