如何在PHP中添加第二个'else'?

问题描述 投票:1回答:3

直到最后一行我的代码工作正常,

如果第一个链接断开,第2页将打开,

如果第2页被破坏,则应打开第3页。

<?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);
    }
php
3个回答
2
投票

试试这个:

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

2
投票

你应该像这样使用else if

<?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);
    }
?>

1
投票

您正在寻找elseif(PHP 4,PHP 5,PHP 7):

示例代码:

<?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";
}
?>

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

其他方案:

elseif旁边你可以使用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;
}
?>

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

© www.soinside.com 2019 - 2024. All rights reserved.