超级简单布尔函数 - 我做错了什么?

时间:2011-01-13 03:15:08

标签: php function

我不确定为什么我不能让它工作: 一个超级简单的函数,只需要返回true或false:

<?php
function check_for_header_images() {
    if ( file_exists('path/to/file') && file_exists('path/to/file'))  
 return true;
}
?>

它不会返回true:

<?php
if(check_for_header_images()) {
    // do stuff
}
?>

...不做事:

<?php
if(!check_for_header_images()) {
    // do stuff
}
?>

......确实做了。

我为函数设置的条件应该返回true。如果我采用相同的if语句并且只是这样做:

<?php
    if ( file_exists('path/to/file') && file_exists('path/to/file'))  {
        //do stuff
    }
?>

有效。 我只是不明白如何编写函数?

1 个答案:

答案 0 :(得分:2)

<?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
  }
?>

或者你可以这样做:

<?php
  function check_for_header_images() {
    return ( file_exists('path/to/file') && file_exists('path/to/file'));
  }
?>

另外,if语句中的!意味着相反。这意味着if (!false)为真,if (!true)为假

相关问题