1

A newbie style question, I'm afraid. I was wondering if someone might tell me what best practice is in the following situation.

Should it be...

function submitted_data()
{
 //Testing user data
 if(userdata != correctlyentered)
 {
   display_form(errors);
 }
 //Submit verified data to server
}

In which case, what's the correct syntax when calling display_form from within submitted_data, because at the moment it continues to submit the data to the server when I don't want it to.

Or should it be...

function submitted_data()
{
 //Testing user data
 if(userdata != correctlyentered)
 {
   display_form(errors);
 }
 else
 {
 //Submit verified data to server
 }
}

Or does it not make any difference?

Thanks.

4
  • is it me or are those two code samples exactly the same? Commented Mar 29, 2011 at 14:48
  • i think this need more explanation - what is "Submit data to server"? this is php, this should already be on the server. Commented Mar 29, 2011 at 14:49
  • They're not the same, no. I've just simplified the question, though, so maybe that will help. Thanks. Commented Mar 29, 2011 at 14:49
  • 1
    @oezi, It's a remote server done via an API. Commented Mar 29, 2011 at 14:49

5 Answers 5

2

Your second example is valid:

function submitted_data();
{
 //Testing user data
 if(userdata != correctlyentered)
 {
   display_form(errors);
 }
 else
 {
 //Submit verified data to server
 }
}

To "exit" a function, use return. It could have been done like this:

function submitted_data();
{
 //Testing user data
 if(userdata != correctlyentered)
 {
   display_form(errors);
   return;
 }
 //Submit verified data to server
}
Sign up to request clarification or add additional context in comments.

2 Comments

+1 Excellent. Thanks for the straightforward answer. It seems to me that the "else" version would be more efficient than the "return" version(?). Either way, thanks a lot.
@Django: They're probably very similar. I prefer using return in error handling because it makes it clear that if false: don't continue. In short, you shouldn't worry about performance, but readability here (although both methods are readable, so it's a matter of personal preference).
1

If you don't use the else statement and don't write a return statement inside the if, the first example will submit data to the server in every case.

Comments

1

If you want to get out of a function, you need to return.

Usually, if you're getting out of a function because you detected a problem - you'll return false so you can code around what happens if the function sends you back a 0/false result.

If your function works as you'd like, use return true so you can code around what to do when the function did what it was supposed to with no problems.

Of course you can return other things - personally I try to avoid using 'echo' in functions so that I can choose to either keep or output the data in different situations.

So:

function myFunction() {
  $result = "something happened";
  return $result;
}

echo myFunction();

Would be preferable to:

function myFunction() {
  $result = "something happened";
  echo $result;
  return true;
}

Because I can call myFunction just to test it without having to display the results, or I can choose to display the results. Two birds killed, one stone used.

Comments

0

First off, do not put semicolons after the function declarations.

Anyway, this depends entirely on what you're trying to do. If you add the else (2nd block), the code will only run if userdata != correctlyentered is FALSE. If you don't add the else (1st block), the code will always run.

Edit: To exit a function, you can use return.

if(userdata != correctlyentered)
{
  display_form(errors);
  return;
}

1 Comment

Oops. It's only pseudo code, but yeah, I removed the semi-colons.
0

I think in this case you should choose the variant with the least code nesting inside {}

function submitted_data()
{
 //Testing user data
 if(userdata != correctlyentered)
 {
   display_form(errors);
   return;
 }

 //Submit verified data to server

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.