Jump to content

return boolean anomaly


shaunmckinnon
Go to solution Solved by shaunmckinnon,

Recommended Posts

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.

Link to comment
Share on other sites

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;
			}
Link to comment
Share on other sites

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();
		}
Link to comment
Share on other sites

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);

Link to comment
Share on other sites

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();
		}
Link to comment
Share on other sites

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 by 0xMatt
Link to comment
Share on other sites

  • Solution

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.

Link to comment
Share on other sites

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;
Link to comment
Share on other sites

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 by AbraCadaver
Link to comment
Share on other sites

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
Link to comment
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.