Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Dec 2006
    Posts
    57
    Thanks
    2
    Thanked 0 Times in 0 Posts

    If error then continue and not die - possible?

    Hello,
    I have the following code:
    PHP Code:
    $request file_get_contents($request_url) or die("feed not loading"); 
    If the file_get_contents($request_url) doesn't load/respond then the script echoes "feed not loading" and stops.

    I want the script to echo "feed not loading" and then continue and just skip that part of the script that needed the file contents.

    Is that possible?

    Best Regards
    Oskar R

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    The "or die()" syntax is really just shorthand for a condition check for a "false" return, ie. if (!condition) {}. So you can easily do

    PHP Code:
    $request file_get_contents($request_url);
    if (!
    $request) {
        echo 
    "feed not loading";
    } else {
        
    //continue with your coding goodness here


  • #3
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by Fumigator View Post
    The "or die()" syntax is really just shorthand for a condition check for a "false" return, ie. if (!condition) {}. So you can easily do

    PHP Code:
    $request file_get_contents($request_url);
    if (!
    $request) {
        echo 
    "feed not loading";
    } else {
        
    //continue with your coding goodness here
    }
    // now is safe to have code here 
    agree with condition to stop the execution of anything outside of if/else

    PHP Code:
    $request file_get_contents($request_url);
    if (!
    $request) {
        echo 
    "feed not loading";
        exit; 
    // this really die
    } else {
        
    //continue with your coding goodness here
    }
    // now is safe to have code here 
    best regards
    Last edited by oesxyl; 03-08-2008 at 12:22 AM.

  • #4
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,046
    Thanks
    19
    Thanked 42 Times in 42 Posts
    Quote Originally Posted by oesxyl View Post
    agree with condition to stop the execution of anything outside of if/else

    PHP Code:
    $request file_get_contents($request_url);
    if (!
    $request) {
        echo 
    "feed not loading";
        exit; 
    // this really die
    } else {
        
    //continue with your coding goodness here
    }
    // now is safe to have code here 
    best regards
    doesn't want to die....
    PHP Code:
    $request file_get_contents($request_url);
    if (!
    $request) {
        echo 
    "feed not loading"//notify there is an error.
    }

    // now is safe to have code here 

  • #5
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by rafiki View Post
    doesn't want to die....
    PHP Code:
    $request file_get_contents($request_url);
    if (!
    $request) {
        echo 
    "feed not loading"//notify there is an error.
    }

    // now is safe to have code here 
    this is wrong! If $request is false, it display the message and continue with what you think that is safe.
    That is the reason of exit there. I don't correct Fumigator code, I just add the posibility to add code not related with $request to the page.

    Edit:
    I want the script to echo "feed not loading" and then continue and just skip that part of the script that needed the file contents.
    I'm sorry, is my fault, rafiki solution work


    best regards
    Last edited by oesxyl; 03-08-2008 at 04:31 PM.

  • #6
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,122
    Thanks
    2
    Thanked 327 Times in 319 Posts
    Read the OP's post, carefully.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • Users who have thanked CFMaBiSmAd for this post:

    oesxyl (03-08-2008)


  •  

    Posting Permissions

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