1

I have seen programming practices where a string or a boolean can be returned from a function. In such cases, is it recommended to check for empty() and isset() in the if loops, or will just doing a check like if($returnvar) will work or not.

Here's a piece of code I am playing around with. What do you think should be the output and are the checks correct in the if condition?

Thanks,

<?php

function testMe()
{
 try
 {
  $returnText = 'John King Rocks';
  return $returnText;
 }
 catch (Exception $e)
 {
  return false;
 }
}

$str = testMe();
if ($str)
{
  echo $str;
}

?>
3
  • Note, the return false; here is unreachable -- nothing in the try block will cause an exception. Commented Aug 9, 2012 at 17:46
  • true.. but it's just an example, incase the code reaches there.. Commented Aug 9, 2012 at 17:49
  • 1
    empty() can blow up on you - it thinks '0' is empty. don't use it unless you know exactly what you're going to be using it on. Commented Aug 9, 2012 at 17:50

3 Answers 3

4

This code should work (for this specific example of $str), but checking for Boolean in PHP is risky, as you suggested, and should be done with caution, I would suggest (in general) to check it as follows:

if ($str !== false)
{
  echo $str;
}
Sign up to request clarification or add additional context in comments.

Comments

2

What will happen is $str will be type casted to a boolean. So, the string will evaluate to false when it is:

  1. The empty string
  2. The string "0"

Every other value is considered true, which may not be the behavior you desire. So, to answer your questions:

What do you think should be the output?

Well, the return string doesn't match either of the two conditions, so the if statement will evaluate to true and the $str will be echo()'d. (Not to mention that the above code can never produce an Exception).

Are the checks correct in the if?

That depends on the functionality you're looking for. If you want to consider all strings (including "" and "0") to be valid, and to skip the echo() only when the function returns false, then you should check if the return value's equality with !== false. Otherwise, if those two conditions above are acceptable as false, you should be fine.

Comments

2

if is not a loop. It's a conditional. echo false will be coerced to echo '0', so the if check is only necessary if you don't want to print a zero. However, echo null will not print anything at all. Even better would be to return empty string. You should avoid mixing return types. Many other languages don't even allow it.

By the way your exception handling does nothing .. the contents of the try cannot throw an exception. Swallowing exceptions is also bad, and you need a $ before the e.

In answer to your question, there is a phrase "paranoid programming is professional programming." That is, you should do as many checks as possible if you want your application to work consistently (and the if check is good in this case), but it's also important to know what methods you are working with and the expected result. Do you want some other handling if testMe is false? Or do you want to just not print anything?

9 Comments

semantically, it's not a loop. Conceptually, though, it could be considered a "loop" that runs 0 or 1 times.
@cHao I disagree. A loop needs to have the ability to run an indefinite number of times.
@ExplosionPills: Explain a do {stuff} while(0) "loop" then. Or for ($x=0; $x==0; ++$x). These both run a definite number of times (one). The keyword isn't important; a "loop" is just a condition + code to run when the condition's true, and can be made to run zero or one times. When do we stop calling a loop a loop?
@cHao the keyword is important. Can you run a function multiple times? Do you call it a loop?
I, like you, consider a loop to be a marked section of code that is designed to run some (possibly infinite, probably multiple) number of times. But i also see a conditional as a degenerate loop, in the same way a point is a degenerate circle. I wouldn't call it a loop in normal usage, same as i wouldn't normally call a point a circle. But conceptually, it is.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.