如何从php中的函数返回true / false

时间:2013-11-26 08:22:00

标签: php

如何将return true / false用于另一个函数?

<?php    
 class Bet {    
     private $Client;
     private $Secret;
     private $Server;    
     public function __construct() {
         $this->Client = md5(uniqid(rand(), true));
         $this->Secret = md5(uniqid(rand(), true));
         $this->Server = md5(uniqid(rand(), true));
     }    
     public function Bet($Type, $Chance) {    
         if($Type == 'auto') {    
                 $hash  = hash('sha512', $this->Client . $this->Secret . $this->Server);
                 $Result = round(hexdec(substr($hash, 0, 8)) / 42949672.95, 2);

                     if($Result < $Chance) {
                         return true;
                     } else {
                         return false;                      
                     }
         }
     }
 }

&GT;

继承人我一直在努力:

if(isset($_POST['chance'], $_POST['bet'])) {
    print_r($Bet->Bet('auto', $_POST['chance']));

    if($Bet == true)
    {
        return "1";
    } else {
        return "2";
        }    
    }

但我无法进入if状态。

3 个答案:

答案 0 :(得分:0)

试试这个

if($Bet)
{
    return "1";
} else {
    return "2";
    }    
}

答案 1 :(得分:0)

你必须实例化你的课程:

$bet = new Bet($type, $chance);

但是构造函数不能像你一样返回一个值。

尝试这样的事情:

public function betTest() { return what you want; }

[...]

if ($bet->betTest()) { return 1; } else { return 2; }

或使用静态方法:

public static function betTest() { /.../ }

if (Bet::betTest()) { ... }

根据您的代码:

<?php    
 class Bet {    
     private $Client;
     private $Secret;
     private $Server;  
     private $result;  

     public function __construct($Type, $Chance) {    
         $this->Client = md5(uniqid(rand(), true));
         $this->Secret = md5(uniqid(rand(), true));
         $this->Server = md5(uniqid(rand(), true));

         if($Type == 'auto') {    
                 $hash  = hash('sha512', $this->Client . $this->Secret . $this->Server);
                 $Result = round(hexdec(substr($hash, 0, 8)) / 42949672.95, 2);

                     if($Result < $Chance) {
                         $this->result = true;
                     } else {
                         $this->result = false;                      
                     }
         }
     }

     /**
     * @return boolean
     */
     public function isReturnLessThanChance() { return $this->result; }
 }

$bet = new Bet("", "");
if ($bet->isResultLessThanChance()) {
    return 1;
else
    return 2;

答案 2 :(得分:-1)

你也可以试试这个。

if($Bet === true) { return "1"; } else { return "2"; } 

===检查数据类型和值。然后(true !== 1)