PHP:if else语句返回多个结果

时间:2015-11-10 17:19:11

标签: php

我想要实现的是检查多个域的可用性。

这是我的代码:

<?php
//$domain = 'example.com';
$domain = array('example.com', 'foo.com');

if ( checkdnsrr(in_array($domain), 'ANY') ) {
  $echo_checkdnsrr = in_array($domain) . " DNS Record found (checkdnsrr)" . "<br />" . PHP_EOL;
}
else {
  $echo_checkdnsrr = in_array($domain) . " NO DNS Record found (checkdnsrr)" . "<br />" . PHP_EOL;
}

if ( gethostbyname(in_array($domain)) != in_array($domain) ) {
  $echo_gethostbyname = in_array($domain) . " DNS Record found (gethostbyname)" . "<br />" . PHP_EOL;
}
else {
  $echo_gethostbyname = in_array($domain) . " NO DNS Record found (gethostbyname)" . "<br />" . PHP_EOL;
}

$separator = "-------------------------------------------" . "<br />" . PHP_EOL;

print_r($echo_checkdnsrr);
print_r($echo_gethostbyname);
echo $separator;
?>

我想要实现的目标:

example.com DNS Record found (checkdnsrr)
example.com DNS Record found (gethostbyname)
-------------------------------------------
foo.com DNS Record found (checkdnsrr)
foo.com DNS Record found (gethostbyname)
-------------------------------------------

感谢您的帮助!

感谢@fluinc,这是我的最终代码:

$domains = array('example.com', 'foo.com');

foreach ($domains as $domain) {
    if (checkdnsrr($domain, 'ANY') || gethostbyname($domain) != $domain) {
        echo "TAKEN " . $domain . "<br />" . PHP_EOL;
    } else {
        echo "AVAILABLE " . $domain . "<br />" . PHP_EOL;
    }
}

这就是它的回报:

TAKEN example.com
TAKEN foo.com

稍后,它可以非常容易地在Excel中导入,选择空格作为分隔符,并通过查询找出可用的域。

2 个答案:

答案 0 :(得分:1)

使用foreach遍历域名,使用in_array是错误的,不需要。

$domains = array('example.com', 'foo.com');

foreach ($domains as $domain) {
    if (checkdnsrr($domain, 'ANY')) {
        echo $domain . " DNS Record found (checkdnsrr)<br />". PHP_EOL;
    } else {
        echo $domain . " NO DNS Record found (checkdnsrr)<br />". PHP_EOL;
    }

    if (gethostbyname($domain) != $domain) {
        echo $domain . " DNS Record found (gethostbyname)<br />". PHP_EOL;
    } else {
        echo $domain . " NO DNS Record found (gethostbyname)<br />". PHP_EOL;
    }
    echo "-------------------------------------------<br />". PHP_EOL;
}

根据评论更新

$domains = array('example.com', 'foo.com');

foreach ($domains as $domain) {
    if (checkdnsrr($domain, 'ANY') || gethostbyname($domain) != $domain) {
        echo $domain . " TAKEN<br />". PHP_EOL;
    } else {
        echo $domain . " AVAILABLE<br />". PHP_EOL;
    }

    echo "-------------------------------------------<br />". PHP_EOL;
}

答案 1 :(得分:0)

尝试使用foreach()echo处理每个结果:

$domains = array('example.com', 'foo.com');

foreach($domains as $domain){
    if(checkdnsrr(in_array($domain), "ANY")){
        echo in_array($domain) . " DNS Record found (checkdnsrr)" . "<br />" . PHP_EOL;
    } else {
        echo in_array($domain) . " NO DNS Record found (checkdnsrr)" . "<br />" . PHP_EOL;
    }

    if(gethostbyname(in_array($domain)) != in_array($domain)) {
        echo in_array($domain) . " DNS Record found (gethostbyname)" . "<br />" . PHP_EOL;
    } else {
        echo in_array($domain) . " NO DNS Record found (gethostbyname)" . "<br />" . PHP_EOL;
    }

    echo "-------------------------------------------" . "<br />" . PHP_EOL;
}

更短的:

$domains = array('example.com', 'foo.com');

foreach($domains as $domain){
    echo in_array($domain).(checkdnsrr(in_array($domain), "ANY") ? "NO " : "")."DNS Record found (checkdnsrr)" . "<br />" . PHP_EOL;
    echo in_array($domain).(gethostbyname(in_array($domain))!= in_array($domain) ? "NO " : "")."Record found (gethostbyname)" . "<br />" . PHP_EOL;
    echo "-------------------------------------------" . "<br />" . PHP_EOL;
}