对用户定义函数中的 return 语句感到困惑
我对退货声明没什么疑问。
a) 是否必须在用户定义的函数中定义 return 语句?
b) 如果我只定义一个没有任何参数的 return 语句,它仍然有效吗?它会返回空值吗?
c) 以下函数有效吗?
function admin_credential($password = 0, $email = 0) {
if( $password != 0) {
$password = sha1($password);
$query = "UPDATE admins SET password = '$password'";
$result = mysql_query($query);
}
if( $email != 0) {
$query = "UPDATE admins SET email = '$email'";
$result = mysql_query($query);
}
return;
}
i have few question regarding the return statement.
a) is it compulsory to define a return statement in a user defined function.?
b) is it still valid if i just define a return statement without any parameter? will it return the null value?
c) is the following function valid?
function admin_credential($password = 0, $email = 0) {
if( $password != 0) {
$password = sha1($password);
$query = "UPDATE admins SET password = '$password'";
$result = mysql_query($query);
}
if( $email != 0) {
$query = "UPDATE admins SET email = '$email'";
$result = mysql_query($query);
}
return;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不。
是的。
是的,但是
$result
将会丢失,因为您没有返回它。return
并不是真正必要的。No.
Yes.
Yes, but
$result
will be lost because you are not returning it. Thereturn
is not really necessary.有时,您编写的函数不会向被调用函数返回任何内容,例如以漂亮的方式打印多维数组的函数。
是的。省略
return
与不带任何参数的return
相同,均返回NULL
。它在语法上是有效的。但如果返回一个布尔值来标记查询的成功/失败,那就更有意义了。以便调用者知道数据库更新是否顺利进行。
编辑:
查询缺少
WHERE
子句。因此它有效地更新了admins
表中所有用户的密码。No. At times you write functions which do not return anything to the called function, say a function to print a multidimensional array in a pretty way.
Yes. Omitting
return
is same asreturn
without any parameter and both returnNULL
.It is syntactically valid. But it would be more meaningful if you return a boolean value to mark the success/failure of the query. So that the caller knows if the DB update went through fine or not.
EDIT:
The query is missing a
WHERE
clause. So it effectively updated the password of all users in theadmins
table.a) 你不需要在 PHP 函数的末尾返回一个值。这大致相当于 C 的
void
函数。b) 没有值的
return
是有效的,但它可能会让其他人稍后查看您的代码感到困惑,因此将其作为标准做法并不是一个好主意。考虑返回NULL
,这将具有相同的效果。c) 是的,您的函数使用有效的语法。
a) you do not NEED to return a value at the end of a function in PHP. This is roughly equivalent to C's
void
function.b) a
return
of no value is valid, but it can be confusing to other people looking at your code later, so it's a bad idea to make that your standard practice. Consider returningNULL
instead, which will have the same effect.c) Yes, your function uses valid syntax.
当您查看用于用户身份验证的 PHP 表时,需要考虑很多事情。您必须仔细考虑如何在表中存储数据以及如何检索数据。这意味着您需要考虑有多少用户将有权访问,以及有多少用户类型。如果您的用户数量很少(甚至少于 10 个),我建议您不要担心数据库用户身份验证。
您提出的问题已经得到了有效的回答,但我敦促您尝试避免使用许多不必要的功能使您的系统复杂化:保持简单。
When you look at PHP tables for user authentication there are many things to consider. You have to mull over how you want to store the data in your tables and how to retrieve it. This means you need to consider how many users are going to have access, and how many user types. If you have a handful of users like even less than 10, I recommend not bothering with database user authentication.
The questions you posed have already been answered effectively but I would urge you to try and avoid complicating your system with a lot of unnecessary features: keep it simple.