Kerri
Kerri

Reputation: 1211

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?

Upvotes: 0

Views: 495

Answers (1)

Brad Christie
Brad Christie

Reputation: 101614

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

Upvotes: 2

Related Questions