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:
- You shouldn't declare functions within functions.
- Your function will never return something, it's always void since you don't
return
anything. Soempty()
will always evaluate to true.
精彩评论