ohdang888 Posted September 23, 2008 Share Posted September 23, 2008 after a function does its job, i want to to return true if succesful... which i'ev done.. but if it fails, i want it to return false AND return $error, so that i have the error message. How can this be achieved? Thanks. Link to comment https://forums.phpfreaks.com/topic/125402-return-false-and-error/ Share on other sites More sharing options...
DarkWater Posted September 23, 2008 Share Posted September 23, 2008 You can't, but show us the function anyway so maybe we can show an alternative solution. Link to comment https://forums.phpfreaks.com/topic/125402-return-false-and-error/#findComment-648352 Share on other sites More sharing options...
jonsjava Posted September 23, 2008 Share Posted September 23, 2008 simple solution: <?php function test($data){ if ($data == 1){ $data_array[0] = true; } else{ $data_array[0] = false; $data_array[1] = "error found"; } return $data_array; } $test = test(2); if ($test[0] == false){ print $test[1]; } Link to comment https://forums.phpfreaks.com/topic/125402-return-false-and-error/#findComment-648357 Share on other sites More sharing options...
Prismatic Posted September 23, 2008 Share Posted September 23, 2008 You can. <?php function Boo($var) { if($var == "test") { return true; } else { return "This is false"; } } if(Boo() !== true) { echo Boo(); } else { echo "True"; } ?> if(boo() !== true) returns "This is false" if(boo("test") !== true) echos "True" Link to comment https://forums.phpfreaks.com/topic/125402-return-false-and-error/#findComment-648358 Share on other sites More sharing options...
ohdang888 Posted September 23, 2008 Author Share Posted September 23, 2008 <?php function log_in($email, $password){ if(empty($email) and empty($password)){ $error = "Empty username or password"; } $email = clean($email); $password = md5(md5(clean($password))); db("discuss"); $res = sql("SELECT * FROM `users` WHERE upper(email)=upper('$email') and `password`='$password' "); if(mysql_num_rows($res) == 1){ $row = mysql_fetch_array($res); $_SESSION['logged_in'] = true; $_SESSION['user_id'] = $row['user_id']; $_SESSION['username'] = $row['username']; $_SESSION['key'] = $row['key']; //grab friends and put it in session if($row['user_id'] == 1){//if user id is 1 $_SESSION['admin'] = true; } $time = date("Y-m-d H:i:s"); $id = $row['user_id']; sql("DELETE FROM `online_now` WHERE `user_id`='$id' LIMIT 1"); sql("INSERT INTO `online_now` (`user_id`, `time`)VALUES('$id', '$time')"); }else{//end if one row is found $res = sql("SELECT `user_id` FROM `users` WHERE `email`='$email' "); if(mysql_num_rows($res) == 0){ $error = "Email does not exist"; }else{ $error = "Wrong Password"; } }//end else if(isset($error)){ return false; return $error; }else{ return true; } }//end log in ?> Link to comment https://forums.phpfreaks.com/topic/125402-return-false-and-error/#findComment-648366 Share on other sites More sharing options...
trq Posted September 23, 2008 Share Posted September 23, 2008 If I where you I would take a look at exceptions. I would also consider breaking that function down into smaller functions, it does far more than simply log in a user, hence your need for more detailed error messages. Functions should be short, concise and to the point. Link to comment https://forums.phpfreaks.com/topic/125402-return-false-and-error/#findComment-648372 Share on other sites More sharing options...
Prismatic Posted September 23, 2008 Share Posted September 23, 2008 <?php function log_in($email, $password){ if(empty($email) and empty($password)){ $error = "Empty username or password"; } $email = clean($email); $password = md5(md5(clean($password))); db("discuss"); $res = sql("SELECT * FROM `users` WHERE upper(email)=upper('$email') and `password`='$password' "); if(mysql_num_rows($res) == 1){ $row = mysql_fetch_array($res); $_SESSION['logged_in'] = true; $_SESSION['user_id'] = $row['user_id']; $_SESSION['username'] = $row['username']; $_SESSION['key'] = $row['key']; //grab friends and put it in session if($row['user_id'] == 1){//if user id is 1 $_SESSION['admin'] = true; } $time = date("Y-m-d H:i:s"); $id = $row['user_id']; sql("DELETE FROM `online_now` WHERE `user_id`='$id' LIMIT 1"); sql("INSERT INTO `online_now` (`user_id`, `time`)VALUES('$id', '$time')"); }else{//end if one row is found $res = sql("SELECT `user_id` FROM `users` WHERE `email`='$email' "); if(mysql_num_rows($res) == 0){ $error = "Email does not exist"; }else{ $error = "Wrong Password"; } }//end else if(isset($error)){ return false; return $error; }else{ return true; } }//end log in ?> Ok do this <?php function log_in($email, $password){ if(empty($email) and empty($password)) { $error = "Empty username or password"; } $email = clean($email); $password = md5(md5(clean($password))); db("discuss"); $res = sql("SELECT * FROM `users` WHERE upper(email)=upper('$email') and `password`='$password' "); if(mysql_num_rows($res) == 1) { $row = mysql_fetch_array($res); $_SESSION['logged_in'] = true; $_SESSION['user_id'] = $row['user_id']; $_SESSION['username'] = $row['username']; $_SESSION['key'] = $row['key']; //grab friends and put it in session if($row['user_id'] == 1) {//if user id is 1 $_SESSION['admin'] = true; } $time = date("Y-m-d H:i:s"); $id = $row['user_id']; sql("DELETE FROM `online_now` WHERE `user_id`='$id' LIMIT 1"); sql("INSERT INTO `online_now` (`user_id`, `time`)VALUES('$id', '$time')"); } else {//end if one row is found $res = sql("SELECT `user_id` FROM `users` WHERE `email`='$email' "); if(mysql_num_rows($res) == 0){ $error = "Email does not exist"; } else { $error = "Wrong Password"; } }//end else if(isset($error)){ return $error; } else { return true; } }//end log in ?> Then check your function <?php if(log_in(blah, blah) !== true) { /** Function is not true, so it contains our error message */ echo log_in(blah, blah); } else { /** Function returned true */ echo "Success!"; } ?> if it's Link to comment https://forums.phpfreaks.com/topic/125402-return-false-and-error/#findComment-648374 Share on other sites More sharing options...
DarkWater Posted September 23, 2008 Share Posted September 23, 2008 Prismatic, that requires calling the function twice...which is not efficient. Link to comment https://forums.phpfreaks.com/topic/125402-return-false-and-error/#findComment-648380 Share on other sites More sharing options...
Prismatic Posted September 23, 2008 Share Posted September 23, 2008 Prismatic, that requires calling the function twice...which is not efficient. Doesn't require it. $blah = myfunct(); if($blah !== true) Link to comment https://forums.phpfreaks.com/topic/125402-return-false-and-error/#findComment-648386 Share on other sites More sharing options...
ohdang888 Posted September 24, 2008 Author Share Posted September 24, 2008 so is this the best way? $blah = myfunct(); if($blah !== true) Link to comment https://forums.phpfreaks.com/topic/125402-return-false-and-error/#findComment-649132 Share on other sites More sharing options...
trq Posted September 24, 2008 Share Posted September 24, 2008 No, the best way IMO would be to use exceptions. Link to comment https://forums.phpfreaks.com/topic/125402-return-false-and-error/#findComment-649145 Share on other sites More sharing options...
thewooleymammoth Posted September 24, 2008 Share Posted September 24, 2008 im not exactly sure but wouldn't this work? if (scandir('images/')) { echo "function successful"; } else { echo "you suck"; } Link to comment https://forums.phpfreaks.com/topic/125402-return-false-and-error/#findComment-649217 Share on other sites More sharing options...
thewooleymammoth Posted September 27, 2008 Share Posted September 27, 2008 oh uh duh function() or die "error"; echo "function successful"; Link to comment https://forums.phpfreaks.com/topic/125402-return-false-and-error/#findComment-651746 Share on other sites More sharing options...
Zane Posted September 27, 2008 Share Posted September 27, 2008 that would work Although you can only use it once and for only one function per script. If you die...so to speak. The script terminates on the contrary though you can do this function someFunction() { return false; } someFunction() or print("This function has failed by god"); Link to comment https://forums.phpfreaks.com/topic/125402-return-false-and-error/#findComment-651747 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.