1

I have a function which return a loop, now I want to check if this function returns null or empty

That's the function:

function getCopy($pname){
    function listCopy($block) {
        foreach ($block as $b) {

            echo  '<div class="copy">'.$b->getBlock().'</div>'. "\n";
        }
    }
    $filter=array(
    new DFC(classContent::FIELD_PNAME, $pname, DFC::CONTAINS),
    new DFC(classContent::FIELD_TYPE, 'copy', DFC::CONTAINS),
    );
    $block=classContent::findByFilter(conn(), $filter);
    return listCopy($block);
}

That's my logic:

if( isset (getCopy($pname)) ){
    echo "<label for='copy'>Copy</label><br>"
    ."<textarea name='copy' id='copy' rows='10' cols='60'>".getCopy($pname)
    ."</textarea><br>";
}

The isset doesn't work and neither if(getCopy($pname) != '') does.

Any idea on how to do this?

Thanks in advance

Mauro

1
  • Kind of silly to actually call getCopy() twice isn't it? If getCopy() returns a value for $pname, then call it again with the same argument to display the returned value that you've already calculated once Commented Mar 18, 2011 at 13:04

4 Answers 4

2

use empty or is_null or a combination of both.

Or you could just negate the check by doing if( !getCopy($pname) ) ){ ... } but i'd go with any of the two functions above.

Edit: as deceze noted, you can't directly evaluate the return value with empty, you'll have to assign it to a var first and then pass that var to empty()

$result = getCopy($pname);
if(empty($result)) { ... }
Sign up to request clarification or add additional context in comments.

3 Comments

You can't use empty or isset to check function return values.
You only need empty when checking potentially non-existing variables. For function return values if (getCopy()) or if (!getCopy()) is just fine.
Calling functions as an argument in empty() is now supported in PHP. You will not need to assign it to a variable first in versions 5.5+. For example: if(empty(getCopy($pname))) { . . . } will work just fine.
1

You can't "return a loop" from a function. You can only return values. The listCopy function does not return anything, it just outputs. Hence getCopy doesn't return anything either. Defining a function within a function is usually bad practice as well, you won't be able to call getCopy twice in your case.

I'm not really sure what you're trying to do, but you need to rethink your approach.

1 Comment

Thanks deceze! I used a different approach now, it was very messy! thanks 4 opening my eyes :)
1

The php function isset()is used to determine if a variable is set and is not NULL. So it's normal that it is not working.

Comments

0

empty() is what you are looking for but bare in mind these two points:

  1. You shouldn't declare functions within functions.
  2. Your function will never return something, it's always void since you don't return anything. So empty() will always evaluate to true.

3 Comments

You can't use empty to check function return values.
@deceze: Indeed, he has to assign the return values to a variable first.
Or he can just use !. empty is simply !isset($foo) || !$foo, and you don't need the first part here since the function certainly exists.

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.