[英]PHP - IF statement
function IsEmailRegistered($Email){
global $con;
$result = mysqli_query($con,"SELECT email FROM Users WHERE email = '" . $Email . "'");
if (mysqli_num_rows($result) > 0){
return True;
}else{
return False;
};
};
that's a function that checks if an email is registered before, the function returns 1 if the email is registered and returns nothing (instead of 0) if it's not registered. 这是一个检查电子邮件是否已注册的函数,如果该电子邮件已注册,则该函数返回1,如果未注册,则不返回任何内容(而不是0)。 What I'm missing here?
我在这里想念的是什么?
If you get 1 and 0 as result, you are echoing it. 如果结果为1和0,则表示正在回显。 Your code is probally right, you just misinterpreted it
您的代码可能正确,只是误解了
echo true; // screen will say 1
echo false; // screen will say 0
var_dump($var); // this will give you the value (true) and its type (boolean)
This always returns true or false. 这总是返回true或false。 Also added a limit to your query, you only need 1 hit to check it.
还为您的查询添加了一个限制 ,您只需点击1次即可对其进行检查。 If you need an exact amount of rows, specify it.
如果您需要确切数量的行,请指定它。 Eg when you want 1 row, use
LIMIT 1
. 例如,当您需要1行时,请使用
LIMIT 1
。 When your site grows, this will save precious resources. 当您的站点增长时,这将节省宝贵的资源。
Here you have your functions, optimised (IMO) 在这里,您可以优化功能(IMO)
function IsEmailRegistered($Email){
global $con;
$result = mysqli_query($con,"SELECT email FROM Users WHERE email = '".$Email."' LIMIT 1");
return mysqli_num_rows($result)===1; // this function will return true/false
}
If you want to stick with the if/else, you can use a ternary, the code below will do the exact same thing as your code does: 如果要坚持使用if / else,则可以使用三进制,下面的代码将执行与代码完全相同的操作:
return mysqli_num_rows($result) > 0 ? true : false;
I changed the true/false to lowercase, always lowercase them for consistancy. 我将true / false更改为小写,为了保持一致性,始终将它们小写。 You might encounter a situation where you have to send 'true' or 'false', you dont want to waste time on something that didnt work because you used a capital :)
您可能会遇到这样的情况,您必须发送“ true”或“ false”,您不想因为使用大写字母而浪费时间在不起作用的东西上:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.