views:

90

answers:

4

hi there,

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;
         }
+2  A: 

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.

Pekka
I got it pekka, the return statement is capable of returning the values as well as halting the script. isn't it?
Ibrahim Azhar Armar
@Ibrahim yes, although "halting the script" isn't entirely accurate: It exits the *function* (which probably is what you mean).
Pekka
To expand on @Pekka's comment, `return` means "return to the code which called this function". It isn't required because the previous code automatically resumes at the end of the function. A return value isn't required because the calling code can be resumed whether or not a value is used. The `return` statement is only needed if you *want* to send a value or to terminate your function early.
Ben Blank
@Ben Blan Roger that :)
Ibrahim Azhar Armar
+2  A: 

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.

sleepynate
As to b, return is a control structure, it's perfectly valid and useful to use it at any point when a function is done its job. If nothing is returned from a function and that function can logically finish at several points in the code then a return without a parameter shouldn't be confusing.
tloach
Completely disagree with b). If there's a need to exit the current scope, `return` is one way to go.
chelmertz
`function myfunc() { return; }` is not nearly as clear as `function myfunc() { return NULL; }`, in my humble opinion. Both will cause a syntax error on assignment, but the NULL is explicit about what is happening. Edit for clarity in order?
sleepynate
I would say return null should be used where a function that normally returns something is explicitly returning nothing, and just return should be used for a function that does not normally return anything, but needs to give control back to the calling code.
tloach
+2  A: 

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.

codaddict
+1  A: 

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.

Geekster