Super Simple Boolean Function— what am I doing wrong?
I'm not sure why I can't get this to work: A super simple function that just needs to return true or false:
<?php
function check_for_header_images() {
if ( file_exists('path/to/file') && file_exists('path/to/file'))
return true;
}
?>
It will not return true:
<?php
if(check_for_header_images()) {
// do stuff
}
?>
…doesn't do stuff:
<?php
if(!check_for_header_images()) {
// do stuff
}
?>
…does do stuff.
The conditions I've set for the function SHOULD return true. If I take that same exact if statement and just do this:
<?php
if ( file_exists('path/to/file') && file_exists('path/to/file')) {
//do stuff
}
?>
It works开发者_开发技巧. Do I just not understand how to write a function?
<?php
function check_for_header_images() {
if ( file_exists('path/to/file') && file_exists('path/to/file'))
return true;
return false; // missing the default return when it's false
}
?>
or you could do:
<?php
function check_for_header_images() {
return ( file_exists('path/to/file') && file_exists('path/to/file'));
}
?>
also, the ! in your if statement means opposite. that means if (!false) is true, and if (!true) is false
加载中,请稍侯......
精彩评论