Results 1 to 3 of 3

Thread: php functions

  1. #1
    Join Date
    Jan 2010
    Thanked 0 Times in 0 Posts

    Default php functions

    hi just a quick question here.

    for php functions is it compulsory to return back a value ?


  2. #2
    Join Date
    Jan 2008
    Thanked 628 Times in 624 Posts
    Blog Entries


    No, its not.
    Jeremy |

  3. #3
    Join Date
    Mar 2006
    Illinois, USA
    Thanked 690 Times in 678 Posts


    It depends on what you mean by that. You do not need to return something, but functions work based on the "return" command. Only one return per function, and processing of that function stops without it. If there is no return, the entire function will process every time.
    Quote Originally Posted by
    Note: If the return() is omitted the value NULL will be returned.
    So in that sense you can either pick a return value or have it automatically chosen for you: you cannot have the function skip the return, though it will literally return "nothing".

    NULL will be equivalent to "" and FALSE if using just double equals:
    if (myfunc()==FALSE) { //either it returned false or null, or 0, "", etc }
    Triple equals will reveal the difference between the types:
    if (myfunc()===FALSE) { //it returned specifically FALSE }
    if (myfunc()===NULL) { //it "didn't" return anything, or NULL was specified }
    (=='s opposite is !=, and ==='s opposite is !==)

    This may be confusing, but it's worth knowing so you can respond accordingly to function returns even when there "isn't" one.

    But as Nile said, no, you do not need to specify a return value.
    But do remember that many functions (and most "typical" functions) are based around the idea of a return value: it's the basis of recursion, for example.
    Though it's done very often, using a function without a return is not really the official use for functions.
    function something() { echo 'something'; } is syntactically correct, but a little strange logically (though many/most programmers use this type at least once in a while).
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts