tags:

views:

84

answers:

5

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), $userfile, $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?

A: 

Your function does not return true. Why would you expect 'ok' to be echoed?

Nick
I did not know that was the problem, that is why I was asking lol.
Scarface
Sorry I wasn't meaning to be snarky. I was thinking there was more to it.
Nick
Snarkyness on stackoverflow...never.
The Real Diel
+7  A: 

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 ?

Dan Heberden
I think he meant to `return 'test';`, to prove that non-empty strings evaluate to true.
BlueRaja - Danny Pflughoeft
thanks dan appreciate it, i added return true to make the function true and it evaluates correctly now
Scarface
@blueraja - yeah, i thought so too but true/false seemed a better teaching aid. Perhaps evaluating strings is part 2 of his journey :)
Dan Heberden
when you say evaluating strings, I googled it and got the function eval. Is that what you are referring to?
Scarface
lol no - evaluating, as in evaluating the condition.. more english definition then programatic definition ;) Because return "text"; would still "evaluate" as true. return 45; would evaluate as true;
Dan Heberden
@Scarface: it means that `if('test') { echo 'ok'; }` will echo "ok"
BlueRaja - Danny Pflughoeft
ooo lol thanks guys, I hate coming off as so amateur but I am always learning lol. Thanks again.
Scarface
+1  A: 

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.

JSBangs
+1  A: 

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;
}
Eric Petroelje
thanks eric appreciate it
Scarface
A: 

Your test() function does not return a value - I would not expect this to work. Try returning something from it, such as TRUE.

Justin Ethier