Jump to content

Recommended Posts

Can you return twice in a php function?

 

Is this allowed?

 

function update_personal_info($username, $firstname, $lastname, $dateofbirth, $city, $state, $country, $personalweb, $credentials, $specialties, $email, $mail_status, $capitalavailable = null, $phonenumber)
{
$firstname			= mres(htmlentities(strip_tags($firstname)));
$lastname			= mres(htmlentities(strip_tags($lastname)));
$dateofbirth		= mres(htmlentities(strip_tags($dateofbirth)));
$city				= mres(htmlentities(strip_tags($city)));
$state				= (!empty($state)) ? (int)$state : null;
$country			= (int)$country;
$personalweb		= mres(htmlentities(strip_tags($personalweb)));
$credentials		= mres(htmlentities(strip_tags($credentials)));
$specialties		= mres(htmlentities(strip_tags($specialties)));
$email				= mres(htmlentities(strip_tags($email)));
$mail_status		= (int)$mail_status;
$phonenumber		= mres(htmlentities(strip_tags($phonenumber)));
$capitalavailable	= (int)$capitalavailable;

	$sql = "UPDATE `users` SET
				`firstname` 	= '{$firstname}',
				`lastname`		= '{$lastname}',
				`city` 			= '{$city}',
				`state` 		= '{$state}',
				`country` 		= '{$country}',
				`email`			= '{$email}',
				`phonenumber` 	= '{$phonenumber}',
				`personalweb` 	= '{$personalweb}',
				`credentials` 	= '{$credentials}',
				`specialties` 	= '{$specialties}',
				`mail_status` 	= '{$mail_status}'
			WHERE `username` 	= '${username}'";

    if ($capitalavailable != null)
{
	$uid = $GLOBALS['user_info']['uid'];

	$sql2 = "UPDATE `investor_info` SET
				`capital_available`	= '{$capitalavailable}'
			WHERE `uid` = '${uid}'";
}

return mysql_query($sql) or die(mysql_error());
return mysql_query($sql2) or die(mysql_error());
}

Link to comment
https://forums.phpfreaks.com/topic/240774-return-twice/
Share on other sites

per the manual (emphasis added)

If called from within a function, the return() statement immediately ends execution of the current function, and returns its argument as the value of the function call. return() will also end the execution of an eval() statement or script file.

 

I hope you can deduce the answer from that.'

 

 

Although I see some logical problems with that function. You have the 2nd query wrapped in an IF statement. So, the mysql_query() call for that query will fail if that IF statement is false. You should never, knowingly, try to run an empty/invalid query.

Link to comment
https://forums.phpfreaks.com/topic/240774-return-twice/#findComment-1236679
Share on other sites

per the manual (emphasis added)

If called from within a function, the return() statement immediately ends execution of the current function, and returns its argument as the value of the function call. return() will also end the execution of an eval() statement or script file.

 

I hope you can deduce the answer from that.

 

Yes,  I understand, but how can I return more than one query? 

Link to comment
https://forums.phpfreaks.com/topic/240774-return-twice/#findComment-1236680
Share on other sites

Yes,  I understand, but how can I return more than one query? 

 

You are not returning a query. You are running UPDATE queries. Since you are using an "or die()" condition on the query calls (instead of properly handling them) you would ONLY EVER return from this function IF BOTH QUERIES successfully ran. So, you don't need to return anything. If there are errors the "or die()" statements will prevent any further execution of the script. So, let the function run, if the function completes, then you know both queries ran.

Link to comment
https://forums.phpfreaks.com/topic/240774-return-twice/#findComment-1236694
Share on other sites

Yes,  I understand, but how can I return more than one query? 

 

You are not returning a query. You are running UPDATE queries. Since you are using an "or die()" condition on the query calls (instead of properly handling them) you would ONLY EVER return from this function IF BOTH QUERIES successfully ran. So, you don't need to return anything. If there are errors the "or die()" statements will prevent any further execution of the script. So, let the function run, if the function completes, then you know both queries ran.

 

I didn't realize that.  I thought you always had to return something.  Am I completely wrong about this?  How do you know when to return data and when not to?

Link to comment
https://forums.phpfreaks.com/topic/240774-return-twice/#findComment-1236818
Share on other sites

Yes,  I understand, but how can I return more than one query? 

 

You are not returning a query. You are running UPDATE queries. Since you are using an "or die()" condition on the query calls (instead of properly handling them) you would ONLY EVER return from this function IF BOTH QUERIES successfully ran. So, you don't need to return anything. If there are errors the "or die()" statements will prevent any further execution of the script. So, let the function run, if the function completes, then you know both queries ran.

 

I didn't realize that.  I thought you always had to return something.  Am I completely wrong about this?  How do you know when to return data and when not to?

 

You return data when you need to have the results of the function in your main code.

Link to comment
https://forums.phpfreaks.com/topic/240774-return-twice/#findComment-1236830
Share on other sites

Yes,  I understand, but how can I return more than one query? 

 

You are not returning a query. You are running UPDATE queries. Since you are using an "or die()" condition on the query calls (instead of properly handling them) you would ONLY EVER return from this function IF BOTH QUERIES successfully ran. So, you don't need to return anything. If there are errors the "or die()" statements will prevent any further execution of the script. So, let the function run, if the function completes, then you know both queries ran.

 

I didn't realize that.  I thought you always had to return something.  Am I completely wrong about this?  How do you know when to return data and when not to?

 

You return data when you need to have the results of the function in your main code.

 

I see... thank you.

Link to comment
https://forums.phpfreaks.com/topic/240774-return-twice/#findComment-1236832
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.