true和false在php中的实际作用是什么


What does true false actually do in php

我见过很多php函数&很多php脚本,我总是能找到

function check() {
    if(isset($_POST['example'])){
        return true;
    } else {
        return false;
    }
}

这是真的吗;做假的?false是否会阻止查询进一步执行?

事实上,我有一个登录页面,如果在数据库中找不到用户,我想停止执行,比如:

if(mysqli_num_rows($query) !=1) {
    // if result is not 1 then executing must be stop here, hello should not be echo
}
echo "hello";

再往下是更多的脚本,只有当结果是1 时才应该执行

根据http://php.net/manual/en/function.return.php-for those of you who think that using return in a script is the same as using exit note that: using return just exits the execution of the current script, exit the whole execution.

所以我在本地主机上尝试了一个代码

$a = 1;
if($a == 0){
echo "Its 0"; }
else{ return; }
echo "hi";

在wrw return false之后,hi这个词没有被执行&当我删除return false时,hi字被执行

现在我将告诉你我真正想知道的

 $query3 =mysqli_query($connecDB, "SELECT * FROM users WHERE username='$username'");
 if(mysqli_num_rows($query3) != 1)
   {
      $er1 = "Username doesn't exist"; 
    }
else{
$query4 ="SELECT * FROM users WHERE email='$email'";
$result1=mysqli_query($connecDB, $query3);
if(mysqli_num_rows($result1) != 1)
   {
      $er1 = "Email doesn't exist"; 
   } else {
    // do this
    }
    }

现在,您可以看到上面我已经将if语句使用到else语句中,并将更多if语句用于else语句,这使得我的php脚本非常复杂&很难理解

我只是想知道什么是更好的方式来执行如下所示的脚本

$query3 =mysqli_query($connecDB, "SELECT * FROM users WHERE username='$username'");
     if(mysqli_num_rows($query3) != 1)
       {
          $er1 = "Username doesn't exist"; 
        }
    else{ // stop the execution here & just print this $er1 on the page without exit or die }

由于下面的示例,我想停止执行

 $a = 1;
if($a == 0){
echo "Its 0 "; }
else{ //if it's not 0 then it means user is not registered do not echo echo hi below }
echo "hi";

现在hi总是执行

使function停止执行的是控制结构returntruefalseBoolean变量,也分别表示为10
if(function that returns a boolean !=1) 

只有当function that returns a booleantrue1)时,if才会执行


了解有关返回和布尔变量的更多信息


请注意,由于安全问题,mysql_*现在已从PHP7开始弃用。建议您切换到mysqli_*PDO扩展。