Advanced Search

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,103
    Thanks
    18
    Thanked 615 Times in 611 Posts
    Blog Entries
    5

    Default

    No, its not.

  3. #3
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    11,785
    Thanks
    225
    Thanked 657 Times in 645 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>| Deutsch | italiano | español | português | català | un peu de français | Ninasoma Kiswahili | 日本語の学生でした。| درست العربية

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
  •