Reply to Re: Returning NULL vs. returning FALSE

Your name:

Reply:


Posted by Erwin Moller on 11/18/05 12:59

Thomas Mlynarczyk wrote:

> Hi,
>
> It seems to be a generally adopted convention to have a function return
> FALSE in case of an error. But if a function is supposed to return a
> boolean anyway, one cannot distinguish anymore between the "normal" FALSE
> and the "error" FALSE. So why not using NULL instead to indicate an error?
> Are there drawbacks I am not aware of?
>
> Greetings,
> Thomas

As Micha wote: Now you cannot distinguish between an 'error NULL' and 'real
NULL'.

And what about a function that could return NULL and false, both as usefull
values?

So if you find yourself in such a situation, be creative.
eg: always return your value in an array (only one element big if needed),
OR let it return false.

function myFunc($bla){
// do stuff
if (succeeded){
return array("theresult" => false);
// or return array("theresult" => array(1,4,78));
// or whatever your function produces
} else {
return false; // or NULL, or whatever suits your needs
}
}

In that way you can just check the returnvalue for being an array, and know
you have a real result (as found in "result")

That is how I solved that problem once. Maybe not very elaborated, but it
does the trick.

Regards,
Erwin Moller

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация