【问题标题】:get_status() function returns 1 instead of true or false, why?get_status() 函数返回 1 而不是 true 或 false,为什么?
【发布时间】:2012-08-27 09:17:00
【问题描述】:

在下面的代码中,我的网站类中的 get_status() 方法返回 1 而不是我想要的 true 或 false。谁能告诉我为什么?我认为这可能是我课堂上的一个错误,我不确定这行代码在 get_status() 方法中是否是好的做法?

$httpcode = $this->get_httpcode();

当我回显 $siteUp 时,无论我将网址设置为 http://www.google.com 还是 http://www.dsfdsfsdsdfsdfsdf.com,它始终为 1

我对面向对象的 php 相当陌生,这是我第一次自学课程,这是我正在构建的学习 oop 的示例。目的是检查一个网站的状态,并根据httpcode判断它是up还是down。

您在为什么这不起作用方面提供的任何提示都会受到极大的欢迎。提前致谢!

class website {
protected $url;

function __construct($url) {
    $this->url = $url;
}

public function get_url() {
    return $this->url;
}

public function get_httpcode() {
    //get the http status code
    $agent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)";
    $ch=curl_init();
    curl_setopt ($ch, CURLOPT_URL,$this->url);
    curl_setopt($ch, CURLOPT_USERAGENT, $agent);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt ($ch, CURLOPT_VERBOSE,false);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSLVERSION, 3);
    $page=curl_exec($ch);
    $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    return $httpcode;
}

public function get_status() {
    $httpcode = $this->get_httpcode();
    if ($httpcode>=200 && $httpcode<400) {
         $siteUp = true;
    } else {
        $siteUp = false;
    }
    return $siteUp;
}
}

// create an instance of the website class and pass the url
$website = new website("http://www.google.com");
$url = $website->get_url();
$httpcode = $website->get_httpcode();
$siteUp = $website->get_status();
echo "site up is set to: " . $siteUp;

【问题讨论】:

  • 您是否尝试过先回显$httpcode
  • 为什么不只是return $httpcode&gt;=200 &amp;&amp; $httpcode&lt;400;
  • 除了http代码之外,检查curl_exec的值也是明智的(在你的情况下,变量$page)。 $page 的值在失败时为 false,

标签: php oop class


【解决方案1】:

1 就是 PHP 将 "true" 转换为字符串的方式

<?php echo true; ?>

将显示 1。

在 PHP 中,针对 1 进行测试和针对 true 进行测试几乎是一回事。

$siteUp = $website->get_status() ? "true" : "false";

将使它成为一个字符串供您显示...但是您不能测试它的真实性,因为“true”和“false”都是有效的字符串,并且会给您一个布尔值 TRUE。

【讨论】:

    【解决方案2】:

    您正在返回一个布尔值,它是 TRUEFALSE(但不是 单词 true 或 false)。

    您既可以返回一个字符串,也可以在将其附加到 echo 语句之前对其进行转换:

    例如:

    public function get_status() {
        $httpcode = $this->get_httpcode();
        if ($httpcode>=200 && $httpcode<400) {
         $siteUp = 'true';
        } else {
        $siteUp = 'false';
        }
        return $siteUp;
    }
    

    或者,如果您想将其保留为返回的布尔值,您可以使用一个非常简单的函数将其转换为这样的字符串:

    public function showBool($myBool)
    {
        return ($myBool) ? 'True' : 'False';
    }
    
    $someVar=false;
    echo showBool($someVar);
    

    【讨论】:

      【解决方案3】:

      作为一个简单的练习,尝试运行以下代码并亲自查看:

      <?php 
          echo true;
      ?>
      

      您要做的是打印一个布尔值,并期望一个字符串“true”或“false”。你可以这样做:

      $booleanVal? "true":"false";
      

      【讨论】:

        【解决方案4】:

        避免打印出true/false。使用 if 语句查看它实际返回的内容。 你期待$siteUp 打印什么?

        if($siteUp) { 
            echo "Up"; 
        } else { 
            echo "Down"; 
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2020-02-04
          • 2021-03-21
          • 1970-01-01
          • 1970-01-01
          • 2019-12-07
          • 2019-01-09
          • 1970-01-01
          相关资源
          最近更新 更多