views:

126

answers:

3

I just realized that you can't just use an if statement on a function, for example this doesn't work:

function sayHello()
{
    echo "Hello World";
}

if(sayHello())
    echo "Function Worked";
else
    echo "Function Failed";

I also saw that a function can't be put as the value of a variable. So how can I do an if statement to check if a function has executed properly and display it to the browser?

+3  A: 

It's not working since sayHello() doesn't return anything place return true in there or something.

lemon
return true works, thanks. But the problem of the function repeating itself inside the if statement is present here too. It says "Hello WorldHello World" in the browser.
Bruce
GreenMatt
@Bruce you're probably calling the function twice.
lemon
A: 
if (sayHello() === FALSE)
    echo "Function Failed";
else
    echo "Function Worked";
fastcodejava
Thanks, this works. Just one thing though, the function is repeated again inside the if statement, so there is "Hello WorldHello World" displayed in the browser.Can this be done without the function repeating itself inside the if statement.
Bruce
I don't know why "Hello World" is repeating. If you call `sayHello()`, it will print "Hello World". I am not sure what you want to do. If you want to print "Hello World" why don't you just call `sayHello()`. Btw shouldn't you up vote since it worked?
fastcodejava
just curious - FOUR '='??? Two I know, three I know ...
Mawg
@mawg Corrected typo.
fastcodejava