2016-05-13 95 views
0

实际上做我见过很多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"; 

拧返回false单词后喜不执行&当我删除返回false则喜字被执行

现在,我会告诉你我真的想知道

$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 
    } 
    } 

现在你见上面我已经使用,如果语句成其他语句和更多,如果声明到其他,这使得我的PHP脚本非常复杂&很难理解

我只是想知道什么是bett呃像图所示

$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"; 

现在喜总是执行

+3

这一切都在手册http://php.net/manual/en/function.return。php –

+0

在你以后的情况下,你可以使用if(check()){然后你有$ _POST ['example']设置并且可以执行一些其他需要这个变量的脚本}。如果您使用DRY编程方法 –

+0

'exit'并且die()会停止执行,那么使用这些函数总是更好。 –

回答

2

是什么让function停止执行是执行控制结构return,truefalseBoolean变量,也分别表示为10


if(function that returns a boolean !=1) 

如果function that returns a booleantrue1)的if将只执行


了解更多关于returnBoolean variables


请注意:由于安全问题,现已弃用,因为PHP7。建议您切换到mysqli_*PDO扩展名。

+0

如果我使用退出和死亡页面将退出&空白页将显示,什么可能是最好的方式?目前我正在使用if(mysqli_num_rows($ query)!= 1){} else {echo“hi”} –

+0

你想要做什么?花点时间思考,更新你的问题将尽可能简明扼要。 –

+0

请检查我编辑的问题 –

相关问题