Jump to content

Question about returning true and false values


Go to solution Solved by kicken,

Recommended Posts

I was just wondering what is the point of returning a false value out of a function when PHP will assume that something is false when it something is empty or has not been set. For photograph function below it returns false based on 2 condition, but my question is what is the difference if I put "return false" or if I wouldn't have put anything.

 

Thanks

  public function attach_file($file) {
		// Perform error checking on the form parameters
		if(!$file || empty($file) || !is_array($file)) {
		  // error: nothing uploaded or wrong argument usage
		  $this->errors[] = "No file was uploaded.";
		  return false;
		} elseif($file['error'] != 0) {
		  // error: report what PHP says went wrong
		  $this->errors[] = $this->upload_errors[$file['error']];
		  return false;
		} else {
			// Set object attributes to the form parameters.
		  $this->temp_path  = $file['tmp_name'];
		  $this->filename   = basename($file['name']);
		  $this->type       = $file['type'];
		  $this->size       = $file['size'];
			// Don't worry about saving anything to the database yet.
			return true;

		}
	}

returning false is not the same as not returning anything. Because PHP is loosely typed you can get similar effects but you can also get very different effects. It depends on what you're doing with the result of the function.

 

http://us3.php.net/manual/en/language.types.type-juggling.php

http://us3.php.net/manual/en/types.comparisons.php

returning false is not the same as not returning anything. Because PHP is loosely typed you can get similar effects but you can also get very different effects. It depends on what you're doing with the result of the function.

 

http://us3.php.net/manual/en/language.types.type-juggling.php

http://us3.php.net/manual/en/types.comparisons.php

Thanks for the response. Just one last question. What do you mean by "Loosely typed"?

returning false is not the same as not returning anything. Because PHP is loosely typed you can get similar effects but you can also get very different effects. It depends on what you're doing with the result of the function.

 

http://us3.php.net/manual/en/language.types.type-juggling.php

http://us3.php.net/manual/en/types.comparisons.php

Also lets say for a function like below, why would it need to return "false" when all its doing is setting an message in the error array?

if(!$file || empty($file) || !is_array($file)) {
		  // error: nothing uploaded or wrong argument usage
		  $this->errors[] = "No file was uploaded.";
		  return false;

The point of returning true and false is for checks. Basically you can do things like

 

 

function isLogged($var)
{
 if ($var == 'foo')
 {
  return true;
 }
 else
 {
  return false;
 }
}

 

and then you can do:

 

 

if (isLogged('foo'))
{
 echo 'Was logged.';
}
else
{
 echo 'Not logged';
}

 

If you change the foo in the if statement to something else (outside the function), it'll say not logged because the if statement is getting a false return. As they said, you may see similar functionality between returning false and returning nothing due to how php is done.

  • Solution

What do you mean by "Loosely typed"?

Loosely typed means the language will automatically convert data from one type to another in order to accomplish what needs done. In a strictly typed language you would have to manually convet the data to the appropriate type.

 

Being loosely typed has some nice benefits of not having to code a bunch of extra stuff just to convert types, and generally makes it easier to work with. It can also however introduce issues if the conversion doesn't happen as one might expect. There are some notable examples of this in PHP, such as:

0 == "true" is true, because "true" gets convert to an integer(0) then you have 0==0.

"1987654321987654321" == "1987654321987654321827" might be true, depending on if you have a 32-bit or 64-bit version of PHP. Because both strings are purely numeric PHP converts them to numbers and compares them, but on a 32-bit system the numbers are too large and end up being the same after conversion.

 

The above, and other potential issues is why the === operator exists which prevents PHP from doing a type conversion.

 

Mostly though, if your function is going to return something at all, it should always return something. Returning a value on success but nothing on a failure is bad practice, even if it ends up working in PHP. By explicitly returning false then areas using that function can determine for sure if something failed vs just getting back a false-like value. For a boolean function (one that returns either true or false) it's not that big of an issue but if your function returns say an integer normally, there is potential for a valid return value (0) to be considered false. For instance, strpos returns an index on success (and 0 is a perfectly valid index) or false on false.

The point of returning true and false is for checks. Basically you can do things like

function isLogged($var)
{
 if ($var == 'foo')
 {
  return true;
 }
 else
 {
  return false;
 }
}

 

I know that is just an example, but it is an example that can perpetuate bad habits. You should, typically, not use a comparison and then use that comparison to return true/false. Just return the comparison

 

 

function isLogged($var)
{
    return ($var == 'foo');
}

 

function isLogged($var)
{
 if ($var == 'foo')
 {
  return true;
 }
 else
 {
  return false;
 }
}

 

Don't write functions like that. The if/else only adds noise to what you really want to say (you are logged if you pass foo). Instead write them like:

 

function isLogged($var) {
  return $var == 'foo';
}
Which outputs the exact same thing.

Loosely typed means the language will automatically convert data from one type to another in order to accomplish what needs done. In a strictly typed language you would have to manually convet the data to the appropriate type.

 

Being loosely typed has some nice benefits of not having to code a bunch of extra stuff just to convert types, and generally makes it easier to work with. It can also however introduce issues if the conversion doesn't happen as one might expect. There are some notable examples of this in PHP, such as:

0 == "true" is true, because "true" gets convert to an integer(0) then you have 0==0.

"1987654321987654321" == "1987654321987654321827" might be true, depending on if you have a 32-bit or 64-bit version of PHP. Because both strings are purely numeric PHP converts them to numbers and compares them, but on a 32-bit system the numbers are too large and end up being the same after conversion.

 

The above, and other potential issues is why the === operator exists which prevents PHP from doing a type conversion.

 

Mostly though, if your function is going to return something at all, it should always return something. Returning a value on success but nothing on a failure is bad practice, even if it ends up working in PHP. By explicitly returning false then areas using that function can determine for sure if something failed vs just getting back a false-like value. For a boolean function (one that returns either true or false) it's not that big of an issue but if your function returns say an integer normally, there is potential for a valid return value (0) to be considered false. For instance, strpos returns an index on success (and 0 is a perfectly valid index) or false on false.

Oh okay I get it now, and understand the whole thing about PHP being a loosely typed . Like 0 can also mean empty if you do a boolean statement, and understand why you have to be explicit on returning values. Thanks a lot for a very clear explanation.

 

I know that is just an example, but it is an example that can perpetuate bad habits. You should, typically, not use a comparison and then use that comparison to return true/false. Just return the comparison

function isLogged($var)
{
    return ($var == 'foo');
}

The reason why its better to write it the way you mentioned above is because its already a comparison operator correct?

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.