2011-01-13 91 views
0

我不知道爲什麼我不能得到這個工作: 一個超級簡單的功能,只是需要返回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。如果我採取同樣的確切聲明,只是這樣做:

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

它的工作。 我只是不明白如何編寫一個函數?

+2

您應該在函數中使用`return file_exists('path/to/file')&& file_exists('path/to/file');`;否則,如果條件爲假,則不返回任何內容。不過,我不知道這是否是問題。 – Jacob 2011-01-13 03:17:30

+0

`file_exists`參數是硬編碼(例如:`path/to/file`)還是它們實際上是變量(例如:`$ foo`)? – netcoder 2011-01-13 03:19:47

回答

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)爲假