eldan88 Posted May 18, 2013 Share Posted May 18, 2013 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; } } Quote Link to comment Share on other sites More sharing options...
Jessica Posted May 18, 2013 Share Posted May 18, 2013 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 Quote Link to comment Share on other sites More sharing options...
eldan88 Posted May 18, 2013 Author Share Posted May 18, 2013 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"? Quote Link to comment Share on other sites More sharing options...
eldan88 Posted May 18, 2013 Author Share Posted May 18, 2013 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; Quote Link to comment Share on other sites More sharing options...
DHood Posted May 18, 2013 Share Posted May 18, 2013 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. Quote Link to comment Share on other sites More sharing options...
DHood Posted May 18, 2013 Share Posted May 18, 2013 More specific to your initial post though (and sorry for the double post). If you were using it with your function you would do something like: if (attach_file($file)) { echo "Success!"; } else { echo "There was an error attaching your file."; } Quote Link to comment Share on other sites More sharing options...
Solution kicken Posted May 18, 2013 Solution Share Posted May 18, 2013 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. Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 18, 2013 Share Posted May 18, 2013 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'); } Quote Link to comment Share on other sites More sharing options...
ignace Posted May 19, 2013 Share Posted May 19, 2013 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. Quote Link to comment Share on other sites More sharing options...
eldan88 Posted May 19, 2013 Author Share Posted May 19, 2013 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. Quote Link to comment Share on other sites More sharing options...
eldan88 Posted May 19, 2013 Author Share Posted May 19, 2013 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? Quote Link to comment 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.