对用户定义函数中的 return 语句感到困惑

发布于 2024-09-18 18:14:52 字数 563 浏览 7 评论 0原文

我对退货声明没什么疑问。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

a) 在用户定义的函数中是否必须定义 return 语句?

不。

b) 如果我只定义一个没有任何参数的 return 语句,它仍然有效吗?它会返回空值吗?

是的。

c) 以下函数有效吗?

是的,但是 $result 将会丢失,因为您没有返回它。 return 并不是真正必要的。

a) is it compulsory to define a return statement in a user defined function.?

No.

b) is it still valid if i just define a return statement without any parameter? will it return the null value?

Yes.

c) is the following function valid?

Yes, but $result will be lost because you are not returning it. The return is not really necessary.

清君侧 2024-09-25 18:14:52

a) 在用户定义的函数中是否必须定义 return 语句?

有时,您编写的函数不会向被调用函数返回任何内容,例如以漂亮的方式打印多维数组的函数。

b) 如果我只定义一个没有任何参数的 return 语句,它仍然有效吗?它会返回空值吗?

是的。省略 return 与不带任何参数的 return 相同,均返回 NULL

c) 以下函数有效吗?

它在语法上是有效的。但如果返回一个布尔值来标记查询的成功/失败,那就更有意义了。以便调用者知道数据库更新是否顺利进行。

编辑:

"UPDATE admins SET password = '$password'"

查询缺少WHERE子句。因此它有效地更新了 admins 表中所有用户的密码。

a) is it compulsory to define a return statement in a user defined function.?

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.

b) is it still valid if i just define a return statement without any parameter? will it return the null value?

Yes. Omitting return is same as return without any parameter and both return NULL.

c) is the following function valid?

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:

"UPDATE admins SET password = '$password'"

The query is missing a WHERE clause. So it effectively updated the password of all users in the admins table.

清音悠歌 2024-09-25 18:14:52

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 returning NULL instead, which will have the same effect.

c) Yes, your function uses valid syntax.

情归归情 2024-09-25 18:14:52

当您查看用于用户身份验证的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文