why does if(function_name()) not work?
Hey guys really quick question, I have a simple test that I was doing to prove a point and it does not work like I expected but I am not sure why it does not work.
function test(){
echo 'test';
}
if (test()){
echo 'ok';
}
That was my test. 'ok' is not echoed and I am not sure why. I was testing this because my real code is calling a class method and is also not working.
if($database->addNewUser($user, md5($pass), $userfi开发者_StackOverflowle, $email, $age)){
return 0; //New user added succesfully
}
The method addNewUser executes and does what it should, but the return 0; does not. Anyone have any insight into this?
Because your test function is ECHOING not returning..
function test(){
return true; // what to send back
}
if (test()){ // true was sent back, so.
echo 'ok';
}
will echo 'ok'.
look at $database->addNewUser() - what's it returning ?
Your function test()
doesn't return any value. PHP treats this absence of a value as false, so the body of the if
block is never executed.
With your full example, the problem is either than addNewUser
doesn't return a value, or it's returning a false value.
You need to return something to indicate success. Either of these would work:
function test(){
echo 'test';
return TRUE;
}
or, less desirable, but should still work:
function test(){
echo 'test';
return 1;
}
Your function does not return true. Why would you expect 'ok' to be echoed?
Your test()
function does not return
a value - I would not expect this to work. Try returning something from it, such as TRUE
.
精彩评论