Page 1 of 1

What do you return when a function fails?

Posted: Tue Nov 08, 2005 7:11 pm
by raghavan20
Let's say you have a function to retrieve member id of the user from the user name.
Suppose you do find the user name invalid and you are not able to return the member id, do you return FALSE, 0 or appropriate error messages.

I used to return FALSE so far, but when I tried to echo the returned FALSE, I don't get anything it returns empty string not FALSE.

example scenario

Code: Select all

echo call_me(3); //This echo call returns no value,,,the return value is EMPTY instead of FALSE
echo call_me(10);//This returns 1 instead of TRUE,,,is this desirable as well...

function call_me($number){
if ($number == 10) return TRUE;
else return FALSE
}
1. If return TRUE actually returns 1, when is it possible to use "call_me(10) === TRUE",,,this kind of statement?
2. Do you feel the behaviors desirable??? Advise me the best coding practices....Thanks

Posted: Tue Nov 08, 2005 7:32 pm
by feyd
false is actually returned.. it just doesn't "echo" naturally...

Posted: Tue Nov 08, 2005 11:13 pm
by m3mn0n
Well the "false" it returns isn't a string variable, it's a boolean variable.

Anytime you see a function returning either true or false in the way you showed, it won't actually echo those two words, it'll just make it so that you can identify whether or not the process was successful (1 or true) or a failure (0 or false).

See: http://php.net/manual/en/language.types.boolean.php

And PHP naturally interchanges 1 and true and 0 and false in regards to this sort of thing.

See: http://php.net/manual/en/language.types ... ggling.php