get_status() function returns 1 instead of true or

2019-07-21 22:19发布

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();

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

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;

标签: php oop class
4条回答
乱世女痞
2楼-- · 2019-07-21 22:32

As a simple exercise, try running the below code and see for yourself:

<?php 
    echo true;
?>

What you are trying to do is print a boolean value, and expect a string "true" or "false". You could do something like:

$booleanVal? "true":"false";
查看更多
Fickle 薄情
3楼-- · 2019-07-21 22:39

Avoid printing out true/false. Use an if-statement to see what it actually returns. What are you expecting $siteUp to print?

if($siteUp) { 
    echo "Up"; 
} else { 
    echo "Down"; 
}
查看更多
冷血范
4楼-- · 2019-07-21 22:49

You are returning a boolean, which is either TRUE or FALSE (but not the words true or false).

You can either return a string instead or convert it before you append it to the echo statement:

For example:

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

Or if you want to keep it as a Boolean being returned, you can use a really simple function to convert it to a string like this:

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

$someVar=false;
echo showBool($someVar);
查看更多
我欲成王,谁敢阻挡
5楼-- · 2019-07-21 22:53

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

<?php echo true; ?>

Will display 1.

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

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

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.

查看更多
登录 后发表回答