Nesting if else statements in PHP to validate a URL

Posted by John on Stack Overflow See other posts from Stack Overflow or by John
Published on 2012-10-06T03:28:06Z Indexed on 2012/10/06 3:37 UTC
Read the original article Hit count: 82

Filed under:

I'm currently writing up a function in order to validate a URL by exploding it into different parts and matching those parts with strings I've defined. This is the function I'm using so far:

function validTnet($tnet_url) {
    $tnet_2 = "defined2";
    $tnet_3 = "defined3";
    $tnet_5 = "defined5";
    $tnet_7 = "";

    if($exp_url[2] == $tnet_2) {
        #show true, proceed to next validation

        if($exp_url[3] == $tnet_3) {
        #true, and next

                if($exp_url[5] == $tnet_5) {
                #true, and last

                        if($exp_url[7] == $tnet_7) {
                        #true, valid
                        }
                }
            }
    } else {
        echo "failed on tnet_2";
    }
}

For some reason I'm unable to think of the way to code (or search for the proper term) of how to break out of the if statements that are nested.

What I would like to do check each part of the URL, starting with $tnet_2, and if it fails one of the checks ($tnet_2, $tnet_3, $tnet_5 or $tnet_7), output that it fails, and break out of the if statement. Is there an easy way to accomplish this using some of the code I have already?

© Stack Overflow or respective owner

Related posts about php