return statement

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rusty Wright

    return statement

    What happens when you take the value returned by a function that
    doesn't return a value? For example,

    function myfunc($myvar) {
    if ($myvar == "xyzzy")
    return(1);

    return;
    }

    $abc = myfunc("def");

    I know from my days of c programming that this would be very bad to
    do. But php gives you so rope I'm wondering if you could just do an
    isset($abc) to see if myfunc returned a value.

    If so then we could write (scary) code where on an error condition
    instead of returning something like -1 you just don't return a value.
  • Andy Hassall

    #2
    Re: return statement

    On 17 Jul 2003 12:29:54 -0700, Rusty Wright <rusty@socrates .Berkeley.EDU>
    wrote:
    [color=blue]
    >What happens when you take the value returned by a function that
    >doesn't return a value? For example,
    >
    > function myfunc($myvar) {
    > if ($myvar == "xyzzy")
    > return(1);
    >
    > return;
    > }
    >
    > $abc = myfunc("def");
    >
    >I know from my days of c programming that this would be very bad to
    >do. But php gives you so rope I'm wondering if you could just do an
    >isset($abc) to see if myfunc returned a value.
    >
    >If so then we could write (scary) code where on an error condition
    >instead of returning something like -1 you just don't return a value.[/color]

    Ask PHP...

    <pre>
    <?php
    function f() {
    return;
    }
    var_dump(f());
    ?>
    </pre>

    Outputs:

    NULL




    --
    Andy Hassall (andy@andyh.co. uk) icq(5747695) (http://www.andyh.co.uk)
    Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)

    Comment

    • Rusty Wright

      #3
      Re: return statement

      I prefer using isset(). If a function has an error it just uses
      return; and you do an isset() to see if there was an error.

      The question wasn't meant as an "is this possible" but more of an idea
      to discuss to see what people thought of it.

      Comment

      • Andy Hassall

        #4
        Re: return statement

        On 17 Jul 2003 13:14:42 -0700, Rusty Wright <rusty@socrates .Berkeley.EDU>
        wrote:
        [color=blue]
        >I prefer using isset(). If a function has an error it just uses
        >return; and you do an isset() to see if there was an error.[/color]

        Well... more accurate would probably be is_null()

        isset will return FALSE for NULL, since it's set to NULL, not 'not set'.
        (erm..)

        <?php
        function f() {
        }
        $x = f();
        if (!isset($x)) {
        print "not isset\n";
        }
        if (is_null($x)) {
        print "is_null\n" ;
        }
        ?>

        not isset
        is_null
        [color=blue]
        >The question wasn't meant as an "is this possible" but more of an idea
        >to discuss to see what people thought of it.[/color]

        I prefer the usual style of returning 'something' for success, or Boolean
        false for failure - which you can check for using === (so you don't mistake a
        valid 0 or empty-string return).

        --
        Andy Hassall (andy@andyh.co. uk) icq(5747695) (http://www.andyh.co.uk)
        Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)

        Comment

        Working...