Tuesday, October 28, 2008

PHP: Always have Return on function

I had once experienced and almost freaked out locating a bug on my script, just to find out that it was just a no returning value or return on my function. The lesson I've learned here is to always use return inside functions even if it does not have to return something. This is a good programming practice to exercise. As much as possible use return();

Let me give some examples where the function will immediately return to the line from which it was called. Here are they:

(1) Use echo
function helloworld()
{
// scripts....
echo "hello World";
}


(2) Return boolean value (just if necessary)
function helloworld()
{
// scripts......

return true;
// or false
}


(3) if function will not return anything then just use return
function helloworld()
{
// scripts....
return;
}

Hope it helps everyone. Thanks and God Bless us all!!!!!

No comments: