ben
ben

Reputation: 65

How to add second 'else' in PHP?

Until the last line my code works fine,

If the first link is broken, page 2 will open,

If page number 2 is broken, then page 3 should open.

<?php
    $clientproiptv = file_get_contents('/clientproiptv.txt', true);
    $paliptv = file_get_contents('/paliptv.txt', true);
    $url1 = 'http://web.com/1';
    $url2 = 'http://web.com/2';
    $url3 = 'http://web.com/3';
    if(get_headers($url1)) {
        header('Location:'.$url1);
    } else {
        header('Location:'.$url2);
    } 
    // ** from here i need help **
    else {
        header('Location:'.$url3);
    }

Upvotes: 1

Views: 127

Answers (3)

K.B
K.B

Reputation: 885

Try this:

   if(get_headers($url1))
    {
       header('Location:'.$url1);
    }
    else if(get_headers($url2)){
       header('Location:'.$url2);
    }
    else{
     header('Location:'.$url3);
    }

Upvotes: 2

Ronnie Oosting
Ronnie Oosting

Reputation: 1252

You are looking for elseif (PHP 4, PHP 5, PHP 7):

Example code:

<?php
if ($a > $b) {
    echo "a is bigger than b";
} elseif ($a == $b) {
    echo "a is equal to b";
} else {
    echo "a is smaller than b";
}
?>

Documentation: http://php.net/manual/en/control-structures.elseif.php

Other solution:

Next to elseif you could use switch (PHP 4, PHP 5, PHP 7):

<?php
switch ($i) {
    case "apple":
        echo "i is apple";
        break;
    case "bar":
        echo "i is bar";
        break;
    case "cake":
        echo "i is cake";
        break;
}
?>

Documentation: http://php.net/manual/en/control-structures.switch.php

Upvotes: 1

Gal Silberman
Gal Silberman

Reputation: 3894

You should use else if like so:

<?php
    $clientproiptv = file_get_contents('/clientproiptv.txt', true);
    $paliptv = file_get_contents('/paliptv.txt', true);
    $url1 = 'http://web.com/1';
    $url2 = 'http://web.com/2';
    $url3 = 'http://web.com/3';
    if(get_headers($url1)){
        header('Location:'.$url1);
    } else if(get_headers($url2) {
        header('Location:'.$url2);
    } else {
        header('Location:'.$url3);
    }
?>

Upvotes: 2

Related Questions