shaunmckinnon Posted September 5, 2013 Share Posted September 5, 2013 Hello: I've just started learning OOPHP, and I love it!!! However, I've come across a strange issue. I'm trying to return a boolean value from my ternary statement. It will not return the boolean. I can return a string literal, an integer, anything else you can think of, but not a boolean. I'm curious if someone could explain why. Here's the code: // delete the row required $sql = "DELETE FROM $table WHERE id = $id"; if($conn -> query($sql)) { try { $num = ($conn -> affected_rows > 0) ? true : false; return $num; } catch (Exception $e) { echo '<br>Caught exception: ', $e -> getMessage(), '<br>'; } } else { return false; } Any help is appreciated. Thank you so much in advance. Quote Link to comment https://forums.phpfreaks.com/topic/281901-return-boolean-anomaly/ Share on other sites More sharing options...
akphidelt2007 Posted September 5, 2013 Share Posted September 5, 2013 Well I don't know the correct terminology but just from experience, false is no value. So if it returns false, it returns an empty value. Return true returns 1. So depending on how you are checking for this "return" you might not be using it the correct way. Quote Link to comment https://forums.phpfreaks.com/topic/281901-return-boolean-anomaly/#findComment-1448361 Share on other sites More sharing options...
kicken Posted September 5, 2013 Share Posted September 5, 2013 (edited) what makes you think you can't return a boolean? Also, there's no need for the ternary operator there, you can just do: return $conn->affected_rows > 0; Edited September 5, 2013 by kicken Quote Link to comment https://forums.phpfreaks.com/topic/281901-return-boolean-anomaly/#findComment-1448362 Share on other sites More sharing options...
shaunmckinnon Posted September 5, 2013 Author Share Posted September 5, 2013 Well I don't know the correct terminology but just from experience, false is no value. So if it returns false, it returns an empty value. Return true returns 1. So depending on how you are checking for this "return" you might not be using it the correct way. False should return as "0", as True returns as "1". Either way, it doesn't return. what makes you think you can't return a boolean? Also, there's no need for the ternary operator there, you can just do: return $conn->affected_rows > 0; Thanks for the help on getting rid of the ternary. I've tried a crazy amount of things and now I'm stuck in a circle of bad practice. lol However, even with your change, it still won't return the boolean. I still get nothing. Here is the current code: $sql = "DELETE FROM $table WHERE id = $id"; if($conn -> query($sql)) { try { return $conn -> affected_rows > 0; } catch (Exception $e) { echo '<br>Caught exception: ', $e -> getMessage(), '<br>'; } } else { return false; } Quote Link to comment https://forums.phpfreaks.com/topic/281901-return-boolean-anomaly/#findComment-1448363 Share on other sites More sharing options...
shaunmckinnon Posted September 5, 2013 Author Share Posted September 5, 2013 I don't know if this makes a difference, but here is the whole method: public function deleteIt($id) { $conn = $this -> connect_open(); // set the table $table = $this -> table; // delete the row required $sql = "DELETE FROM $table WHERE id = $id"; if($conn -> query($sql)) { try { return $conn -> affected_rows > 0; } catch (Exception $e) { echo '<br>Caught exception: ', $e -> getMessage(), '<br>'; } } else { return false; } $conn = $this -> connect_close(); } Quote Link to comment https://forums.phpfreaks.com/topic/281901-return-boolean-anomaly/#findComment-1448364 Share on other sites More sharing options...
akphidelt2007 Posted September 5, 2013 Share Posted September 5, 2013 False should return as "0", as True returns as "1". Either way, it doesn't return. False can be used as 0, but it does not return 0. It returns an empty string. You can use (int) false to get the zero. Quote Link to comment https://forums.phpfreaks.com/topic/281901-return-boolean-anomaly/#findComment-1448365 Share on other sites More sharing options...
kicken Posted September 5, 2013 Share Posted September 5, 2013 False should return as "0", as True returns as "1". Either way, it doesn't return. No, false returns as false, and true returns as true. If you're talking about what they would be after converting them to a string (such as by echo'ing them) then false become "" (empty string) and true becomes "1". If you want to see exactly what a value is (type and contents) use var_dump($var); Quote Link to comment https://forums.phpfreaks.com/topic/281901-return-boolean-anomaly/#findComment-1448366 Share on other sites More sharing options...
.josh Posted September 5, 2013 Share Posted September 5, 2013 what are you doing to compare the returned value Quote Link to comment https://forums.phpfreaks.com/topic/281901-return-boolean-anomaly/#findComment-1448367 Share on other sites More sharing options...
shaunmckinnon Posted September 5, 2013 Author Share Posted September 5, 2013 False can be used as 0, but it does not return 0. It returns an empty string. You can use (int) false to get the zero. Thank you. I never knew that, but it makes perfect sense. what are you doing to compare the returned value I'm not doing anything yet. I just want a returned value of either true or false. It's so bizarre. I could just return 1 or 0 and It works. But the second I change it to true or false, even with (int)false, it refuses to obey. Here is the adjusted code: public function deleteIt($id) { $conn = $this -> connect_open(); // set the table $table = $this -> table; // delete the row required $sql = "DELETE FROM $table WHERE id = $id"; if($conn -> query($sql)) { try { return (int)$conn -> affected_rows > 0; } catch (Exception $e) { echo '<br>Caught exception: ', $e -> getMessage(), '<br>'; } } else { return (int)false; } $conn = $this -> connect_close(); } Quote Link to comment https://forums.phpfreaks.com/topic/281901-return-boolean-anomaly/#findComment-1448369 Share on other sites More sharing options...
.josh Posted September 5, 2013 Share Posted September 5, 2013 well how do you know what you are(n't) getting unless you're looking at it somehow? Quote Link to comment https://forums.phpfreaks.com/topic/281901-return-boolean-anomaly/#findComment-1448370 Share on other sites More sharing options...
0xMatt Posted September 5, 2013 Share Posted September 5, 2013 (edited) return $conn -> affected_rows > 0; Will always return false. return ($conn->affected_rows > 0) ? true : false; is what you want. Edit: Yeah, I had messed up my return statement which threw me off as well. Edited September 5, 2013 by 0xMatt Quote Link to comment https://forums.phpfreaks.com/topic/281901-return-boolean-anomaly/#findComment-1448371 Share on other sites More sharing options...
shaunmckinnon Posted September 5, 2013 Author Share Posted September 5, 2013 Never mind. I var_dump'd the value in my file that's getting the returned value and it comes up bool(false). $dbman = new DBManipulate; $dbman -> set_Table('users'); $result = $dbman -> deleteIt(6); var_dump($result); I need to learn more. Cheers and thanks for the help! Quote Link to comment https://forums.phpfreaks.com/topic/281901-return-boolean-anomaly/#findComment-1448372 Share on other sites More sharing options...
akphidelt2007 Posted September 5, 2013 Share Posted September 5, 2013 return $conn -> affected_rows > 0; Will always return false. return ($conn->affected_rows > 0) ? true : false; is what you want. That's not true. It will return true or false Quote Link to comment https://forums.phpfreaks.com/topic/281901-return-boolean-anomaly/#findComment-1448373 Share on other sites More sharing options...
shaunmckinnon Posted September 5, 2013 Author Share Posted September 5, 2013 return $conn -> affected_rows > 0; Will always return false. return ($conn->affected_rows > 0) ? true : false; is what you want. Thanks!!! That is exactly what I want. My brain is fried. I've been doing this for 12 hours straight. Time to unplug. Quote Link to comment https://forums.phpfreaks.com/topic/281901-return-boolean-anomaly/#findComment-1448374 Share on other sites More sharing options...
Solution shaunmckinnon Posted September 5, 2013 Author Solution Share Posted September 5, 2013 Fixed with this: public function deleteIt($id) { $conn = $this -> connect_open(); // set the table $table = $this -> table; // delete the row required $sql = "DELETE FROM $table WHERE id = $id"; if($conn -> query($sql)) { try { return (int)(($conn->affected_rows > 0) ? true : false); } catch (Exception $e) { echo '<br>Caught exception: ', $e -> getMessage(), '<br>'; } } else { return (int)false; } $conn = $this -> connect_close(); } Thanks again for the help. Quote Link to comment https://forums.phpfreaks.com/topic/281901-return-boolean-anomaly/#findComment-1448375 Share on other sites More sharing options...
akphidelt2007 Posted September 5, 2013 Share Posted September 5, 2013 Thanks!!! That is exactly what I want. My brain is fried. I've been doing this for 12 hours straight. Time to unplug. These statements are equivalent... return ($conn->affected_rows > 0) ? true : false; return $conn->affected_rows > 0; Quote Link to comment https://forums.phpfreaks.com/topic/281901-return-boolean-anomaly/#findComment-1448376 Share on other sites More sharing options...
akphidelt2007 Posted September 5, 2013 Share Posted September 5, 2013 Fixed with this: public function deleteIt($id) { $conn = $this -> connect_open(); // set the table $table = $this -> table; // delete the row required $sql = "DELETE FROM $table WHERE id = $id"; if($conn -> query($sql)) { try { return (int)(($conn->affected_rows > 0) ? true : false); } catch (Exception $e) { echo '<br>Caught exception: ', $e -> getMessage(), '<br>'; } } else { return (int)false; } $conn = $this -> connect_close(); } Thanks again for the help. There's no reason to return a boolean if you want a value. When I need to get 1s and 0s, I do... return $conn->affected_rows > 0 ? 1 : 0; Quote Link to comment https://forums.phpfreaks.com/topic/281901-return-boolean-anomaly/#findComment-1448377 Share on other sites More sharing options...
AbraCadaver Posted September 5, 2013 Share Posted September 5, 2013 (edited) That's not the best answer, it's sloppy and strange, and well kinda dumb! I think you need to decide what you want and why. Most people want true or false, just because you can't echo it doesn't mean you need inegers. return (int)(($conn->affected_rows > 0) ? true : false); //same as return (int)($conn->affected_rows > 0); //same as return ($conn->affected_rows > 0) ? 1 : 0; For that matter boolean: return (bool)$conn->affected_rows; Or hell, just this if you do a loose test (it's either 0 or not): return $conn->affected_rows; Edited September 5, 2013 by AbraCadaver Quote Link to comment https://forums.phpfreaks.com/topic/281901-return-boolean-anomaly/#findComment-1448378 Share on other sites More sharing options...
.josh Posted September 5, 2013 Share Posted September 5, 2013 would also like to point out that true will only equate to 1 and false will only equate to 0 or '' if they are loosely compared. If you strictly compare them then they do not equate. For example: if (true == 1) echo "true == 1 "; // this will echo if (true === 1) echo "true === 1 "; // this will not echo if (false == 0) echo "false == 0 "; // this will echo if (false === 0) echo "false === 0 "; // this will not echo if (false == '') echo "false == '' "; // this will echo if (false === '') echo "false === '' "; // this will not echo Quote Link to comment https://forums.phpfreaks.com/topic/281901-return-boolean-anomaly/#findComment-1448383 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.