开发者

php check function return

开发者 https://www.devze.com 2023-02-17 11:55 出处:网络
I have a function which return a loop, now I want to check if this function returns null or empty That\'s the function:

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->ge开发者_StackOverflow中文版tBlock().'</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


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)) { ... }


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.


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.


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.
0

精彩评论

暂无评论...
验证码 换一张
取 消