我见过很多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.
所以我在我的localhost上尝试了一个代码
$a = 1;
if($a == 0){
echo "Its 0"; }
else{ return; }
echo "hi";
在wring返回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 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总是被执行
答案 0 :(得分:2)
function
停止执行的原因是控制结构return
,true
或false
是Boolean
个变量,也表示为1
和0
分别。
if(function that returns a boolean !=1)
if
仅在function that returns a boolean
为true
1
时才会执行
请注意,由于安全问题,mysql_*
现已弃用PHP7
。建议您切换到mysqli_*
或PDO
扩展名。