dongxun3424 2014-11-08 19:39
浏览 52
已采纳

php bool函数返回字符串而不是bool

I have a php Form class designed to validate form input. However, I've run into some unexpected behavior when implementing an isEmailEmpty() function. Here is my code:

class Form {
var $userEmail;
var $userPassword;
var $hashedPassword;
var $requiredPassLength = 6;

public function __construct($userEmail, $userPassword) {
    $this->userEmail = $this->cleanInput($userEmail);
    $this->userPassword = $this->cleanInput($userPassword);
}

public function cleanInput($dataField) {
    $dataField = trim($dataField);
    $dataField = stripslashes($dataField);
    $dataField = htmlspecialchars($dataField);
    return $dataField;
}

public function isEmailEmpty() {
    return ($this->userEmail == "" || $this->userEmail == null) ? true : false;
}

public function isPasswordEmpty() {
    return ($this->userPassword == "" || $this->userPassword == null) ? true : false;
}

public function isEmailValid() {
    return filter_var($this->userEmail, FILTER_VALIDATE_EMAIL);
}

public function isPasswordValid() {
    return (strlen($this->userPassword) >= $this->requiredPassLength) ? true : false;
}

public function hashPassword() {
    return $this->hashedPassword = password_hash($this->userPassword, PASSWORD_BCRYPT);
}
// More functions down here...

Now, lets say I've instantiated an object in the following way:

$userEmail = "example@example.com";
$userPassword = "rootatoot";
$form = new Form($userEmail, $userPassword);

Since my isEmailValid() function returns a bool, I expect to receive a boolean return value, i.e, true/false or 1/0 upon excecution. However, that is not the case. Instead, when the expression evaluates to true, I get a string output of the email value tested. For instance, given the object instantiated above, the statement echo $form->isEmailValid() outputs example@example.com. Running gettype($form->isEmailValid) returns string, which also was not expected. What's even more curious: the isPasswordValid() function does NOT behave this way. Running $form->isPasswordValid() returns 1 if the password is in fact valid. Running gettype($form->isPasswordValid()) returns boolean, as expected. It's also interesting that my code still works as expected. For instance, the block

if ($form->isEmailValid()) {
      echo "Valid email";
} else {
      echo "Invalid email";
}

always gives the correct output. Even though my code is running as expected, it's still important to understand what's happening behind the scenes and why. So, my questions regarding this behavior are:

  1. What is responsible for a bool function returning a string? For instance, was this by design or a byproduct of some other process?
  2. Are there any scenarios where this unexpected return value can cause a problem? Or does the system understand this evaluation and always handles it as accordingly? My code works fine now, but I don't think its far-fetched to anticipate unexpected return values causing some issues sooner or later.

I appreciate any information you can provide. Thanks!

  • 写回答

2条回答 默认 最新

  • duanguoyin7008 2014-11-08 19:44
    关注

    Since my isEmailValid() function returns a bool

    Well, no, it doesn't.

    From the documentation of filter_var:

    Returns the filtered data, or FALSE if the filter fails.

    Your if logic still worked because a non-empty string is "truthy", meaning it will be treated the same as true when used as a condition.

    In order to have isEmailValid actually return a boolean, you can change your code to this:

    public function isEmailValid()
    {
       return filter_var($this->userEmail, FILTER_VALIDATE_EMAIL) !== FALSE;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?