Jump to content

variable from function


avo

Recommended Posts

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

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.

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/>";
}
}

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.