Results 1 to 3 of 3

Thread: php functions

  1. #1
    Join Date
    Jan 2010
    Posts
    13
    Thanks
    3
    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 ?

    thanks

  2. #2
    Join Date
    Jan 2008
    Posts
    4,167
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    No, its not.
    Jeremy | jfein.net

  3. #3
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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.
    Also:
    Quote Originally Posted by php.net
    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

Bookmarks

Posting Permissions

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