get_status()函数返回1而不是true或false,为什么? [英] get_status() function returns 1 instead of true or false, why?

查看:178
本文介绍了get_status()函数返回1而不是true或false,为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中,我的网站类中的get_status()方法返回1而不是true或false,就像我想要的那样。任何人都可以告诉我为什么吗?我认为这可能是我的类中的一个错误,我不知道这行代码是否是get_status()方法中的良好做法。

In the code below the get_status() method in my website class returns 1 instead of true or false like I want it to. Can anyone tell me why please? I think it's probably a mistake in my class, I'm not sure if this line of code is good practice within the get_status() method?

$ httpcode = $ this - > get_httpcode();

$httpcode = $this->get_httpcode();

当我回显$ siteUp时,它总是1,无论我把URL作为 http://www.google.com http://www.dsfdsfsdsdfsdfsdf。 com

When I echo $siteUp it is always 1 whether I put the url as http://www.google.com or http://www.dsfdsfsdsdfsdfsdf.com

我对面向对象的php很新,这是我第一次教自学课,这是一个我正在学习oop的例子。它的设计是检查一个网站的状态,并说它是上升还是下降基于httpcode。

I am fairly new to object orientated php and this is my first time teaching myself classes and this is an example I'm building to learn oop. It's desigened to check a website status and say whether it's up or down based on the httpcode.

任何提示你有为什么这不工作会大大收到。提前感谢!

Any tips you have in why this isn't working would be greatly recieved. Thanks in advance!

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;


推荐答案

1是PHP如何将true string

1 is just how PHP converts "true" to a string

<?php echo true; ?>

将显示1。

,测试1和测试对真实大多是相同的事情。

In PHP, testing against 1 and testing against true are mostly the same thing.

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

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

will make it a string for you to display... but you then can't test against it for truth, since both "true" and "false" are valid strings and will give you a boolean TRUE.

这篇关于get_status()函数返回1而不是true或false,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆