unemployment Posted June 30, 2011 Share Posted June 30, 2011 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()); } Quote Link to comment https://forums.phpfreaks.com/topic/240774-return-twice/ Share on other sites More sharing options...
Psycho Posted June 30, 2011 Share Posted June 30, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/240774-return-twice/#findComment-1236679 Share on other sites More sharing options...
unemployment Posted June 30, 2011 Author Share Posted June 30, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/240774-return-twice/#findComment-1236680 Share on other sites More sharing options...
Pikachu2000 Posted June 30, 2011 Share Posted June 30, 2011 Return an array. Quote Link to comment https://forums.phpfreaks.com/topic/240774-return-twice/#findComment-1236681 Share on other sites More sharing options...
Psycho Posted June 30, 2011 Share Posted June 30, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/240774-return-twice/#findComment-1236694 Share on other sites More sharing options...
unemployment Posted June 30, 2011 Author Share Posted June 30, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/240774-return-twice/#findComment-1236818 Share on other sites More sharing options...
KevinM1 Posted June 30, 2011 Share Posted June 30, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/240774-return-twice/#findComment-1236830 Share on other sites More sharing options...
unemployment Posted June 30, 2011 Author Share Posted June 30, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/240774-return-twice/#findComment-1236832 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.