SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Evangelist GeekSupport's Avatar
    Join Date
    May 2002
    Location
    Southern California
    Posts
    408
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    (how?) die(); on a foreach error

    is there a way to kill a script if an error occured?

    ex:

    PHP Code:
    function loop_me($array$obj)
    {
        if (
    emtpy($obj->name))
            die(
    'Name not set in object');

        foreach (
    $array as $a)
        {
            if (
    $a 1)
                echo 
    'BOOM!';
        }
        return 
    1;

    if the first if statement works, the function will return 1 even if !is_array($array). i know this wasn't the best example, but it serves its purpose.

    [edit]
    i guess doing an if statement might work (but looks kinda crude)
    PHP Code:
    function loop_me($array$obj)
    {
        
    $return 0;

        if (
    emtpy($obj->name))
            die(
    'Name not set in object');
        if (
    == 1)
        {
            foreach (
    $array as $a)
            {
                if (
    $a 1)
                    echo 
    'BOOM!';
            }
            
    $return 1;
        }
        return 
    $return;


  2. #2
    SitePoint Wizard
    Join Date
    May 2003
    Location
    Berlin, Germany
    Posts
    1,829
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why not use:

    PHP Code:
    function loop_me($array$obj


        if (empty(
    $obj->name)) 
        {
            die(
    'Name not set in object');
        } 
        else
        {
            foreach (
    $array as $a
            { 
                echo 
    'BOOM!'
            } 
            
        } 
        return 
    true

    ?

  3. #3
    SitePoint Evangelist GeekSupport's Avatar
    Join Date
    May 2002
    Location
    Southern California
    Posts
    408
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    because both parts need to be executed ;-P

    also, if the foreach fails in the else, the function will still return true;

  4. #4
    La la la la la bronze trophy lieut_data's Avatar
    Join Date
    Jun 2003
    Location
    Waterloo, ON
    Posts
    1,517
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm baffled as to where you think the problem is, but won't this work?:

    PHP Code:
    function loop_me($array$obj) {
        if (
    emtpy($obj->name))
            die(
    'Name not set in object');
        if (!
    is_array($array)) 
            die(
    'Array isn\'t an array!');

        foreach (
    $array as $a) {
            if (
    $a 1)
                echo 
    'BOOM!';
        }
        return 
    1;

    My name is Steve, and I'm a super-villian.

  5. #5
    SitePoint Evangelist GeekSupport's Avatar
    Join Date
    May 2002
    Location
    Southern California
    Posts
    408
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes, that code would work, but i thought there might be a better way to globally kill execution of scripts if it encountered a problem. my first example above intentionally left out is_array() thinking there was a way to catch errors. if the script had multiple foreach or other looping functions, it would need to check all the variables before it starts the loop.

    i guess we'll have to error check everything until php5 comes out. thanks for the help everyone

  6. #6
    SitePoint Wizard
    Join Date
    May 2003
    Location
    Berlin, Germany
    Posts
    1,829
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by GeekSupport
    also, if the foreach fails in the else, the function will still return true;
    Hmm..the only way (really?) that a foreach fials is if the argument is not an array. If it's not you could die() it out as lieut showed. If the script dies then it doesn't matter if the function returned true, that's why you can return true; always. Unless I am missing something here...

  7. #7
    La la la la la bronze trophy lieut_data's Avatar
    Join Date
    Jun 2003
    Location
    Waterloo, ON
    Posts
    1,517
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by DarkAngelBGE
    Hmm..the only way (really?) that a foreach fials is if the argument is not an array. If it's not you could die() it out as lieut showed. If the script dies then it doesn't matter if the function returned true, that's why you can return true; always. Unless I am missing something here...
    Or even better, since the function seems to do nothing but fail or return true, just

    PHP Code:
    return; 
    And don't bother witch checking the return value.

    Whey try / catch statements get into full swing, we won't be checking those return values anyway, but will throw errors instead.
    My name is Steve, and I'm a super-villian.

  8. #8
    SitePoint Evangelist GeekSupport's Avatar
    Join Date
    May 2002
    Location
    Southern California
    Posts
    408
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hehe yeah. like i said above, it's not the best of examples. the whole point of this post was: how to stop the rest of the script dead in it's track as soon as an error occured (foreach in this case).

    [edit]
    after thinking it over, i suppose it is best to check for all the different errors. i was hoping i could avoid this.

    my initial thought was that there was some $__ERROR_FOUND variable i could envoke before the return statement.

    PHP Code:
    if ($__ERROR_FOUND) die();
    return 
    1

  9. #9
    La la la la la bronze trophy lieut_data's Avatar
    Join Date
    Jun 2003
    Location
    Waterloo, ON
    Posts
    1,517
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by GeekSupport
    hehe yeah. like i said above, it's not the best of examples. the whole point of this post was: how to stop the rest of the script dead in it's track as soon as an error occured (foreach in this case).
    Ohh -- this was an example (doh!)

    See set_error_handler and trigger_error
    My name is Steve, and I'm a super-villian.

  10. #10
    SitePoint Evangelist GeekSupport's Avatar
    Join Date
    May 2002
    Location
    Southern California
    Posts
    408
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ah! thanks lieut_data, i think this is what i was looking for.


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
  •