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 4 of 4

Thread: try

  1. #1
    New Coder
    Join Date
    Mar 2009
    Location
    Texas
    Posts
    31
    Thanks
    1
    Thanked 2 Times in 2 Posts

    try

    Ok so I want to know if there is a way to try something like in python there is

    try:
    some code
    except: pass

    and basically if the code fails it will skip the rest of the code is there a way to do that in php like i know there is a try{} but i dont see anything that would have an except thing or is there another way of doing this
    ex: include("OMG.txt"); but OMG.txt doesn't exist so instead of showing an error like

    Warning: include(OMG.txt) [function.include]: failed to open stream: No such file or directory in GOD:\OMGNOWAY\really\why_are_u_reading_this\lolz.php on line ##

  • #2
    Regular Coder seco's Avatar
    Join Date
    Nov 2008
    Location
    Oregon
    Posts
    687
    Thanks
    6
    Thanked 79 Times in 77 Posts
    if
    else
    elseif

    http://us.php.net/manual/en/control-structures.if.php

    PHP Code:
    $filename 'foo.txt';

    if (
    file_exists($filename)) {
        echo 
    "The file $filename exists";
    } else {
        echo 
    "The file $filename does not exist";


  • #3
    New Coder
    Join Date
    Mar 2009
    Location
    Texas
    Posts
    31
    Thanks
    1
    Thanked 2 Times in 2 Posts
    wow i totally forgot about that thanks alot

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Warning is an error, not an exception. In order to use a exception handling you need to catch something that is throwable:
    PHP Code:
    function mySqrt($i)
    {
        if (!
    is_int($i) || $i 0)
        {
            throw new 
    InvalidArgumentException('Argument must be a positive integer!');
        }
        return 
    sqrt($i);
    }

    try
    {
        
    mySqrt(-1);
    }
    catch (
    InvalidArgumentException $ex)
    {
        
    printf("Exception: %s\n"$ex->getMessage());

    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)


  •  

    Posting Permissions

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